A String is a sequence of one or more characters enclosed with in single apostrophe ' ' or double apostrophe " "
In python we don't have CHAR data type
A python string may contins any characters
Example:
>>>username="David"
>>>email='dwalker@mail.com'
each character of a python string contains +ve and -ve index values as shown
retriving some portion of a given string
Slice operator:- [start:end+1:step]
start --- 0
end+1 --- length of the string
step --- 1
>>>x = "COMPUTER"
x="COMPUTER"
print(x[0:4])
#prints COMP
print(x[0:8:2])
#prints CMUE
print(x[-3:-8:-1])
#prints TUPMO
print(x[4:])
#prints UTER
print(x[:-3])
#prints COMPU
print(x[-2:])
#prints ER
There are many string handling following are most widely used string handling methods
>>>user="python"
>>>print(user.capitalize())
Python
>>>user="PYTHON
>>>print(user.lower())
python
>>>user="python"
>>>print(user.upper())
PYTHON
>>>mystr="This is python"
>>>print(mystr.find('is'))
2
>>>print(mystr.find('Is'))
-1 #python is case sensitive
>>>print(mystr.find('was'))
-1
>>>mystr="Python is simple,Python is awesome,python is easy'
>>>print(mystr.count('Python'))
2
>>>print(mystr.count('python'))
1
>>>mystr="This is core python"
>>>print(mystr.replace('core','advance'))
This is advance python
>>>mystr="python is simple;python is awesome"
>>>print(mystr.split(';'))
['python is simple' ; 'python is awesome']
>>>mystr="python is simple'
>>>print(mystr.split())
['python' , 'is' , 'simple']
>>>lst=['python' , 'is' ,'simple' ]
>>>print('->'.join(lst))
python->is->simple
>>>print(''.join(lst))
python is simple
>>>username="David Walker"
print(username.startswith("david')
False
print(username.startswith("David")
True
>>>username="David Walker"
print(username.endswith("walker")
False
print(username.endswith("Walker")
True
>>>mystr=" David Walker "
>>>print(mystr)
#white-space at start & end of the value
David Walker
>>>print(mystr.strip())
David Walker
>>>mystr="CompanyX"
>>>print(mystr.isapha())
True
>>>dig=20334
>>>print(dig.isdigit())
True