String Function

Python has built in String Class 'str'. It can be enclosed by either single or double quotes

A string literal can span multiple lines, but there must be a backslash \ at the end of each line to escape the newline. String literals inside triple quotes, """ or ''', can span multiple lines of text.

Python strings are "immutable" which means they cannot be changed after they are created.

Example

st='my name is amit'

Accessing String Value:

st[]

If the index is out of bounds for the string, Python raises an error. 

"+" is used to concatenate string but all string should be string type, if not then we have to make it using str() function.

 A "raw" string literal is prefixed by an 'r' and passes all the chars through without special treatment of backslashes.

String Slices:

Example:

Bio="sonu kumar, Data analyst"

  • To get all character: Bio[:]
  • To get from 0 to 2: Bio[0:3]
  • To get from 1 to 3: Bio[1:4]
  • To get Last String: Bio[-1]
  • To get String from last 5th: Bio[-5:-1]
  • Omit First Index: Bio[:4]
  • When we ommit first index it will start from 0
  • Omit Last Index: Bio[1:]
  • When we ommit last index, it will end at last
  • Length of String: len(Bio)
  • Slice with stride: Bio[::2] it will escape every alternate character
  • Slice with stride: Bio[::-2] first it will reverse the string and then escape every alternate character.

String Method:

Bio.capitalize()

Bio.lower()

Bio.upper()

Bio.title(): co

Bio.swapcase()

Bio.count('a'): count number of occurrence of a in string

Bio.count('a',9): count from specific index

Bio.count('a',9,14): count between range of index

Bio.startswith('s'): check whether string start with given character or not

Bio.startswith('s',4): check whether string start with given character or not from 5th position

Bio.startswith('shjk',4,8): check whether the string start with given character or not from 5th position and check till 9th position.

Bio.endswith('s'): check whether the string ends with a given character or not

Bio.find('kumar'): It returns the index of K when it finds the match otherwise it returns -1

Bio.find('kumar',10): It will search the value after 11th  index and will return the value of index l
Bio.find('kumar',10,14): It will search the value after between 11 to 15th  index and will return the value of index l.
Bio.index('lyst'): It return the index of l if its matches otherwise it will throw error.


Character Classification:

Bio="123bhs"
Bio.isalnum(): Output -  True

Bio="123$bhs"
Bio.isalnum(): Output -  False

Bio="hs"
Bio.isalpha() Output - True

Bio="12kjh"
Bio.isalpha() Output - False

Character Classification:

str.isdigit(): it checks, whether the string have only digit or not.
str.isidentifier():it checks, whether the string have only identifier or not.
iskeyword('def'): it checks, whether the string have only identifier or not.
s.isspace():it checks, whether the string have only space character or not.
space character: \t , \n
str.islower():
str.isupper():

str.istitle():

String Formatting:

str.strip(): remove space from string

str.lstrip(): remove space from left side of string

s.rstrip(): remove space from right side of string

s = 'spam spam spam egg bacon spam spam lobster'

s.replace('spam','spam2')

str='42'

str.zfill(4): it will fill four zero before string

s='spam '

s.ljust(10,'-'): string length will be 10 and spam would be in left and after spam there will be 6 more character '-'.

s.rjust(10,'-'): string length will be 10 and spam would be in right and before spam there will be 6 more character '-'.


Converting Between string and list:

mylist = ['spam', 'egg', 'sausage', 'bacon', 'lobster']

'  '.join(mylist)

Partition and Split:

Partition:

 s = 'egg.spam'

 s.partition('.') : it will check '.' from left side.

s.rpartition(':'): it will check '.' from right side.

 s = 'spam;egg;sausage;bacon;lobster'

 s.split(';') : It will partitioned the string and convert it into List

 str.splitlines(): It will partitioned the string of multiple line and convert it into list.


https://realpython.com/lessons/find-and-seek/

String Formatting

String %