|capitalize||makes 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|
|removesuffix||If the given string ends with given string , removes it and returns new string|
|startswith||is string starts with given substring|
|casefold||Return a version of the string suitable for caseless comparisons.|
|isalnum||Returns true if String contains alpha-numeric otherwise Fase.|
|istitle||If first character is in Upper case returns True other-wise False|
|replace||replaces the given string with all characters of the substring, if count is given then replaces according to count occurances.|
|strip||Removes leading and trailing spaces|
|isalpha||Returns True if all characters are alphabetic otherwise False|
|isupper||All characters are in upper case returns True or False|
|rfind||returns highest index if sub string is found, otherwise -1|
|swapcase||converts all lower case letters to upper case,all upper case letters to lower case|
|count||counts occurance of given character|
|isascii||is string contains all ascii characters|
|join||Concatenate any number of Strings|
|rindex||returns highest index if sub string is found, otherwise raises exception ValueError|
|title||Converts every word starting character as Upper Case|
|isdecimal||Returns True, if string is Decimal String , othwerwise False|
|endswith||checks string ends with specified characters|
|isdigit||does string has only numbers|
|lower||converts all upper case characters to lower case|
|upper||converts all lower case characters to upper case|
|expandtabs||Expands all tabs using spaces and returns a new string|
|isidentifier||Returns True,string is valid Python Identifier|
|lstrip||trims left side spaces|
|zfill||Pad a numeric string with zeros on the left, to fill the field of the given width|
|find||searches the string for a given substring, if found returns lowest index,otherwise -1|
|islower||all characters in the string is lower case returns True else False|
|rstrip||trims right side spaces|
|isnumeric||string has all numbers,return True else False|
|partition||Partition the string into 3 parts|
|split||split based on delimter, returns list|
|isprintable||String returns True, if all characters are consedered in repr() as printable, otherwise False|
|removeprefix||if the string starts with the given string removes it, returns new string|
|splitlines||splits string based on newline character,returns list|
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' >>>
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. >>>init.join('abc') 'a b c'Note: Python String join method , is different from java string join method.
>>> >>> >>>'.'.join(['127','0','0','1']) '127.0.0.1'
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 ,returns index number starting from 0 to length-1, if found otherwise ValueError exception is thrown.
>>>s.index('t') 0 't' is found at index 0(lowest index). 't' found many places in above string, bu returns lowest index >>>s.index('save') 29 'save' substring is found at starting index 29. >>>s.index('zoo') ValueError: substring not found catch the ValueError exception -------------------------------- >>> try: ... s.index('zoo') ... except: print('zoo not found') zoo not found
find method ,returns index number starting from 0 to length-1, if found otherwise -1.
>>>s.find('t') 0 't' is found at index 0(lowest index). 't' found many places in above string, but returns lowest index >>>s.find('save') 29 'save' substring is found at starting index 29. >>>s.find('zoo') -1 substring 'zoo' not found , returns -1
count method ,returns count of substring, if found otherwise 0 occurances.
>>>s.count('t') 2 letter 't' found in 2 places >>>s.count('a') 4 letter 'a' found in 4 places >>>s.count('an') 2 substring 'an' found in 'began' and 'and' >>>s.count('zoo') 0 substring 'zoo' not found in string s
find method returns lowest-index, rfind returns highest-index.
similarly rindex returns highest index.
All these strip methods removes trailing and leading empty spaces in a string
----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 '
-- removes leading spaces using lstrip method >>>" you been fired".lstrip() 'you been fired'
-- removes trailing spaces using rstrip method >>>"you are hired as Senior Manager ".rstrip() 'you are hired as Senior Manager'
These methods changes the aplhabets into upper or lower case.
>>>s='cheetahs are the fastest living land animals' >>>s.upper() 'CHEETAHS ARE THE FASTEST LIVING LAND ANIMALS'
>>>s='CHEETAHS ARE THE FASTEST LIVING LAND ANIMALS' >>>s.lower() 'cheetahs are the fastest living land animals'
>>>sports='tennies' >>>sports.isupper() False >>>sports.islower() True >>>
Checks entire string has numbers only
>>>s='0123456' >>>s.isdigit() True --if string has only digits, then using this method can convert to int type >>>i=int(s)
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.
>>>s='tortoise' >>>s.upper() TORTOISE >>>s.upper().lower() 'tortoise'
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
-- here delimiter is color(:) >>>s='name:sam' >>>s.split(':') ['name','sam'] -- convert INDIA into a list 'I",'N','D','I','A' >>>s='INDIA' >>>' '.join(s).split() ['I",'N','D','I','A']
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']