sampleqa.in tutorials, python tutorial

Python tutorial

Python String Methods

capitalizemakes first character is upper case and rest lower case.
index index returns given sub string's first index
isspace string has atleast one or more spaces , then returns True,otherwise False
removesuffixIf the given string ends with given string , removes it and returns new string
startswithis string starts with given substring
casefold Return a version of the string suitable for caseless comparisons.
isalnumReturns true if String contains alpha-numeric otherwise Fase.
istitleIf first character is in Upper case returns True other-wise False
replacereplaces the given string with all characters of the substring, if count is given then replaces according to count occurances.
stripRemoves leading and trailing spaces
isalphaReturns True if all characters are alphabetic otherwise False
isupperAll characters are in upper case returns True or False
rfindreturns highest index if sub string is found, otherwise -1
swapcaseconverts all lower case letters to upper case,all upper case letters to lower case
countcounts occurance of given character
isasciiis string contains all ascii characters
joinConcatenate any number of Strings
rindexreturns highest index if sub string is found, otherwise raises exception ValueError
titleConverts every word starting character as Upper Case
isdecimalReturns True, if string is Decimal String , othwerwise False
endswithchecks string ends with specified characters
isdigitdoes string has only numbers
lowerconverts all upper case characters to lower case
upperconverts all lower case characters to upper case
expandtabsExpands all tabs using spaces and returns a new string
isidentifierReturns True,string is valid Python Identifier
lstriptrims left side spaces
zfillPad a numeric string with zeros on the left, to fill the field of the given width
findsearches the string for a given substring, if found returns lowest index,otherwise -1
islowerall characters in the string is lower case returns True else False
rstriptrims right side spaces
isnumericstring has all numbers,return True else False
partitionPartition the string into 3 parts
splitsplit based on delimter, returns list
isprintableString returns True, if all characters are consedered in repr() as printable, otherwise False
removeprefixif the string starts with the given string removes it, returns new string
splitlinessplits string based on newline character,returns list

String capitalize, title methods

    capitalize method makes first character as Upper case letter, remaining all lower case
title method , makes every word's first letter as upper case

	>>> sports='gymnastics is a sport'
	>>> sports.capitalize()
	'Gymnastics is a sport'
		>>> game_name='The Expanse: a telltale series, cost is $99.99'
		>>> game_name.title()
		'The Expanse: A Telltale Series, Cost Is $99.99'

String join method

   Initial string which called this method,is going to be inserted between in given string.

	  >>> init=' '
--init string is space character, it is going to stuffed/inserted between each character.
		'a b c'
Note: Python String join method , is different from java string join method.

Insert dot between each item in the list or to form a IP Address note:each item in the list should a string

String find , index, count methods

    find,index count has similar functionality, search for a given character or substring. if it finds returns index , otherwise some methods throw exception, some methods throw -1 etc.,

Choose appropriate method for your requirement. Here is the string

	s = 'they began to hoard food and save money'

index method

     index method ,returns index number starting from 0 to length-1, if found otherwise ValueError exception is thrown.

		't' is found at index 0(lowest index). 't' found many places in above string, bu returns lowest index
		'save' substring is found at starting index 29.
		ValueError: substring not found
		catch the ValueError exception
		>>> try:
		...   s.index('zoo')
		... except: print('zoo not found')
		zoo not found


find method

     find method ,returns index number starting from 0 to length-1, if found otherwise -1.

		't' is found at index 0(lowest index). 't' found many places in above string, but returns lowest index
		'save' substring is found at starting index 29.
		substring 'zoo' not found , returns -1

count method

     count method ,returns count of substring, if found otherwise 0 occurances.

		letter 't' found in 2 places
		4  letter 'a' found in 4 places
		substring 'an' found in 'began' and 'and'
		substring 'zoo'  not found in string s

String rfind, rindex methods

     find method returns lowest-index, rfind returns highest-index.
similarly rindex returns highest index.

String strip,rstrip ,lstrip Methods

    All these strip methods removes trailing and leading empty spaces in a string

String strip method

	----Removes leading and trailing empty spaces and creates a new string 
	>>>s='   they began to hoard   '
	'they began to hoard'

	 -- Removes special symbols like >>>> and <<<<, (leading and trailing), special symbols should be passed to strip method.
	>>> ">>>> This is is email body <<<<<".strip('><')
	' This is is email body '

String lstrip method

	-- removes leading spaces using lstrip method
	>>>"  you been fired".lstrip()
	'you been fired'

String rstrip method

	-- removes trailing spaces using rstrip method
	>>>"you are hired as Senior Manager  ".rstrip()
	'you are hired as Senior Manager'

String upper, lower , isupper,islower,isdigit methods

   These methods changes the aplhabets into upper or lower case.

String upper method

>>>s='cheetahs are the fastest living land animals'

String lower method

	'cheetahs are the fastest living land animals'

String islower,isupper method


String isdigit method

    Checks entire string has numbers only

	--if string has only digits, then using this method can convert to int type

Mix upper and lower methods

     You can mix string methods as a pipe in UNIX or Fluent API in java.
as shown below
here calling upper and lower methods in same statement.


String split and splitlines method

    split & splitlines methods returns a list, split splits based on delimiter, by default delimiter is space, otherwise user specified one. for splitlines delimiter is new line

String split method

	-- here delimiter is  color(:)
	-- convert INDIA into a list  'I",'N','D','I','A'
	>>>' '.join(s).split()

String splitlines method

    splitlines method splits string using newline as delimter, returns list

>>> s="this is first line\nthis is second line\nthis is third line"
>>> s.splitlines()
['this is first line', 'this is second line', 'this is third line']