String Data Type
By: Dr. Nand Kumar Jyotish
Assistant Professor, CSE, MNIT Jaipur
String Data Type
Any sequence of characters within either single quotes or double
quotes is considered as a String.
Syntax:
s='michael'
s="michael"
Eg:
>>> s='''python
is very very
easy'''
print(s)
output: python
is very very
easy'''
We can also use triple single quotes for having single quotes or double
quotes as symbol inside String literal. i.e., if we want to use single quote
(‘) or double quote (“) symbol inside the string literal, then we should
enclose the string literals inside the triple single quote.
Dr. Nand Kumar Jyotish
Eg:
s='This is ' single quote symbol' ==>invalid
s='This is \' single quote symbol' ==>valid
s="This is ' single quote symbol"====>valid
s='This is " double quotes symbol' ==>valid
s="This is \" double quotes symbol" ==>valid
s = 'These are \' single and " double symbols' ==>valid
s='The "Python Notes" by 'michael' is very helpful' ==>invalid
s="The "Python Notes" by 'michael' is very helpful"==>invalid
s='The \"Python Notes\" by \'michael\' is very helpful' ==>valid
s='''The "Python Notes" by 'michael' is very helpful''' ==>valid
Eg:
s='Rahul'
print(s[0]) \\ output: 'R'
print(s[4]) \\ output: 'l'
print(s[-1]) \\ output: 'l'
print(s[10])
IndexError: string index out of range
Note: If we are trying to access characters of a string with out of range index
then we will get error saying : IndexError
Dr. Nand Kumar Jyotish
Output:
D:\python_classes>py [Link]
Enter Some String: Rahul
The character present at positive index 0 and at nEgative index -5 is R
The character present at positive index 1 and at nEgative index -4 is a
The character present at positive index 2 and at nEgative index -3 is h
The character present at positive index 3 and at nEgative index -2 is u
The character present at positive index 4 and at nEgative index -1 is l
2. Accessing characters by using slice operator:
Syntax: s[beginindex: endindex: step]
beginindex: From where we have to consider the slice (substring),
endindex: We have to terminate the slice(substring) at endindex – 1,
step: incremented value.
Note: If we are not specifying the beginning index, then it will be
considered from the beginning of the string.
If we are not specifying end index then it will consider up to end of the
string.
The default value for step is 1.
Dr. Nand Kumar Jyotish
Behaviour of slice operator:
s[begin: end: step]
step value can be either +ve or –ve.
if +ve then it should be forward direction(left to right) and we have
to consider begin to end-1.
if -ve then it should be backward direction(right to left) and we have
to consider bEgin to end+1.
Dr. Nand Kumar Jyotish
print("Malaviya"+"National") #MalaviyaNational
print("NIT"*2) #NITNIT
Eg:
s='michael'
print(len(s)) #7
Dr. Nand Kumar Jyotish
Checking Membership:
We can check whether the character or string is the member of another string or
not by using “in” and “not in” operators.
s='michael'
print('h' in s) #True
print('z' in s) #False
Output:
D:\python_classes>py [Link]
Enter main string: malaviyanationalinstitute
Enter sub string: national
national is found in main string
D:\python_classes>py [Link]
Enter main string: malaviyanationalinstitute
Enter sub string: nit
nit is not found in main string
Dr. Nand Kumar Jyotish
Output:
D:\python_classes>py [Link]
Enter first string: rahul
Enter Second string:rahul
Both strings are equal
D:\python_classes>py [Link]
Enter first string: rahul
Enter Second string: michael
First String is less than Second String
D:\python_classes>py [Link]
Enter first string: rahul
Enter Second string: anil
First String is greater than Second String
Dr. Nand Kumar Jyotish
Dr. Nand Kumar Jyotish
Eg:
1) s="rahulravipavanshiva"
2) print([Link]('l'))#4
3) print([Link]('a',7,15))#10
4) print([Link]('z',7,15))#-1
Dr. Nand Kumar Jyotish
Dr. Nand Kumar Jyotish
Dr Nand Kumar Jyotish
Eg1:
1) s="malaviya national institute"
2) l=[Link]()
3) for x in l:
4) print(x)
Output:
malaviya
national
institute
Dr Nand Kumar Jyotish
Dr Nand Kumar Jyotish
Eg:
print('Rahul786'.isalnum()) #True
print('rahul786'.isalpha()) #False
print('rahul'.isalpha()) #True
print('rahul'.isdigit()) #False
print('786786'.isdigit()) #True
print('abc'.islower()) #True
print('Abc'.islower()) #False
print('abc123'.islower()) #True
print('ABC'.isupper()) #True
print('Learning python is Easy'.istitle()) #False
print('Learning Python Is Easy'.istitle()) #True
print(' '.isspace()) #True
Dr Nand Kumar Jyotish
Eg:
name='rahul'
salary=10000
age=48
print("{} 's salary is {} and his age is {}".format(name,salary,age))
print("{0} 's salary is {1} and his age is {2}".format(name,salary,age))
print("{x} 's salary is {y} and his age is {z}".format(z=age,y=salary,x=name))
Output:
rahul's salary is 10000 and his age is 48
rahul's salary is 10000 and his age is 48
rahul's salary is 10000 and his age is 48
Dr Nand Kumar Jyotish