• A string is a sequence of characters
• str class
• IMMUTABLE STRINGS
• Mutable means changeable and immutable means
unchangeable. Hence, strings are immutable sequences
of characters.
Str1=” Python”
Str1[0]=”t”
print(Str1)
ERROR:
TypeError: ‘str’ object does not support item assignment
INBUILT PYTHON FUNCTIONS FOR STRING
>>> a = “PYTHON”
>>> len(a) #number of characters in string a
6
>>> min(a) #Return smallest character present
‘H’
>>> max(a) #Return largest character present
‘Y’
index[] OPERATOR
>>> S1=”Python”
>>>S1[0] #Access the first element of the string.
‘P’
>>>S1[5] #Access the last element of the String.
‘n’
>>> S=”PYTHON”
>>> S[-1]#Access the last character of a String ‘S’
‘N’
>>> S[-2]
‘O’
>>> S[-3]
‘H’
>>> S[-4]
‘T’
>>> S[-5]
‘Y’
>>> S[-6]#Access the First character of a String ‘S’
‘P’
>>>str1=”Hello”
>>>str2=”Hello”
>>> id(Str1)
53255968
>>>id(Str2)
53255968
TRAVERSING STRING WITH for AND while LOOP
S=”India”
for ch in S:
print(ch,end=””)
Output
India
S=” PYTHONPROGRAMMING”
for ch in range(0,len(S),2
print(S[ch],end=” “)
S=”India”
index=0
while index<len(S):
print(S[index],end=””)
index=index+1
Output
India
STRING OPERATORS
[Link]
>>> S=”IIT-BOMBAY”
>>> S[4:10]#Returns a Subset of a String
‘BOMBAY’
>>>S=”IIT-BOMBAY”
>>> S[0:len(S):2]
>>>’ITBMA’
>>> S=”IIT-MADRAS”
>>> S[::]#Prints the entire String
‘IIT-MADRAS’
>>> S[::-1]
‘SARDAM-TII’#Display the String in Reverse Order
>>>S=”IIT-MADRAS”
>>> S[-1:0:-1]#Access the characters of a string from index -1
>>>’SARDAM-TI’
>>> S[:-1]
#start with the character stored at index 0 & exclude the last
character stored at index -1.
‘IIT-MADRA’
2. +, * and in Operators
>>> S1=”IIT “
>>> S2=”Delhi”
>>> S1+S2
‘IIT Delhi’
>>> S1=”Hello”
>>> S2=3*S1
>>> S2
‘HelloHelloHello’
>>> S1=”Information Technology”
>>> “Technology” in S1
True
>>> “Engineering” in S1
False
>>> S1=”Information Technology”
>> “Hello” not in S1
True
3. String Comparison
>>> S1=”abc”
>>> S2=”abc”
>>> S1==S2
True
>>> S1=”ABC”
>>> S2=”DEF”
>>> S1>S2
False
>>> S1=”AAA”
>>> S2=”AAB”
>>> S2>S1
True
>>> S1=”ABCD”
>>> S2=”abcd”. upper()
>>> S2
‘ABCD’
>>> S1>S2
False
>>> S1>=S2
True
4. Format ()
>>> ‘{} plus {} equals {}’.format(4,5,’Nine’)
‘4 plus 5 equals Nine’
>>>”My Name is {0} and I am from
{1}”.format(“Milinda”,”USA”)
‘My Name is Milinda and I am from USA’
>>> “I am {0} years old.I Love to work on {PC}
Laptop”.format(25,PC=”APPLE”)
‘I am 25 years old.I Love to work on APPLE
Laptop’
5. split() Method
• >>>Str1=”C C++ JAVA Python
• >>>[Link]()
• [‘C,C++,JAVA,Python’]
6. Testing String
• bool isalnum()-Returns True if characters in the string
are alphanumeric and there is at least one character
• bool isalpha() Returns True if the characters in the string
are alphabetic and there is at least one character.
• bool isdigit() Returns True if the characters in the string
contain only digits.
• bool islower() Returns True if all the characters in the
string are in lowercase
• bool isupper() Returns True if all the characters in the
string are in upper case
• bool isspace() Returns true if the string contains only
white space characters.
>>>S=”Python Programming”
>>>[Link]()
False
>>> P=”1Jhon”
>>>[Link]()
True
>>> S=”Programming”
>>>[Link]()
True
>>> S=”1Programming”
>>>[Link]()
False
>>> Str1=”1234”
>>> [Link]()
True
>>> Str2=”123Go”
>>> [Link]()
False
>>> S=”hello”
>>>[Link]()
True
>>> S=”HELLO”
>>>[Link] ()
True
>>> S=” “
>>>[Link]()
True
>>> Str1=”Hello Welcome to Programming
World”
>>> [Link] ()
False
7. Searching Substring in a String
8. Convert string into another string
Q. Replace two chocolates and two cookies by three
chocolates and
three cookies.
>>> S1=”I have brought two chocolates, two cookies
and two cakes”
>>> [Link](“two”,”three”,2)
#Replace only first 2 occurrences of old string
“two” by “three”
‘I have brought three chocolates, three cookies and
two cakes’
9. stripping
10. formatting