STRING MANIPULATION IN PYTHON PROF POOJAB S
 Getting Length of a string  Traversal through a string with a loop  String slices  Strings are immutable  Looping and Counting  The in operator String Manipulation in Python
 String is a sequence of characters.  Enclosed within pair of single/double quotes.  Character has an index Number.  Eg: “Hello World”  Access characters using Index.  Eg: word1='Hello' word2='Hi' x=word1[1] print(x) y=word2[0] print(y) Character H e l l o W o r l D Index 0 1 2 3 4 5 6 7 8 9 10
 End of the string can be accessed using Negative Index.  Eg: “Hello World”  Eg: word=“Hello” x=word[-1] print(x) x=word[-5] print(x) Character H e l l o W o r l Negative Index -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 d
Getting Length of a String using len()  Built-in function  Obtain length of a string.  Eg: word="Python" l=len(word) print(l) Traversal through String with a loop  Extracting every character and performing some action is called Traversal.  It can be done using while or for loop
1. Using for loop s="Python" for i in s: print(i, end=‘t’) 2. Using while loop s="Python" i=0 while i<len(s): #FORWARD TRAVERSAL print(s[i],end='t') i=i+1 s="Python" i=-1 while i>= -len(s): #BACKWARD TRAVERSAL print(s[i],end='t') i=i-1
String Slices  Portion of a string is a string slice.  Extract a required number of characters using colon (:)  Syntax: st[i:j:k]  i is the first index or beginning index or start index  j is the last index or end index. If j index is not present, means slice should be till the end of the string.  k is the stride, to indicate no. of steps incremented. Default value is 1.  Eg: st=“Hello World” print(st[0:5:1])
1. print (st[0:5]) 2. print (st[3:8]) 3. print (st[7:]) 4. print (st[::]) 5. print (st[::2]) 6. print (st[4:4]) 7. print (st[3:8:2]) 8. print (st[1:8:3]) 9. print (st[-4:-1]) 10. print (st[-1]) 11. print (st[:-1]) 12. print (st[:]) 13. print (st[::-1]) 14. print (st[::-2])
 Strings are immutable [cannot change]  To modify string, create a new string.  Eg: 1. s="hello world“ #TypeError: 'str' object does not support item assignment s[3]='t‘ 2. s="hello world" s1=s[:3]+'t'+s[4:] #helto world print(s1) String are immutable
 Using loops we can count the frequency of character.  Eg: w='Book' count=0 for letter in w: if letter=='o': count=count+1 print("The Occurences of Character 'o' is %d"%(count)) Looping and Counting
 Boolean operator takes 2 string operands.  Returns True if 1st operand appears in 2nd Operand. Else False.  Eg: ‘el’ in ‘hello’ #True ‘x’ in ‘hello’ #False ‘EL’ in ‘Hello’ #False The in operator
 Comparison operators like: <,> and == applied to string objects.  Result in True or False  Comparisons happens using ACSII codes  Eg: 1. s='hello' if s=='hello': print('Same') #Same 2. s='hello' if s<='Hello': print('Lesser') else: print('Greater') #Greater String Comparison
 A-Z 65-90  a-z 97-122  0-9 48-57  Space 32  Enter 13

String Manipulation in Python

  • 1.
    STRING MANIPULATION INPYTHON PROF POOJAB S
  • 2.
     Getting Lengthof a string  Traversal through a string with a loop  String slices  Strings are immutable  Looping and Counting  The in operator String Manipulation in Python
  • 3.
     String isa sequence of characters.  Enclosed within pair of single/double quotes.  Character has an index Number.  Eg: “Hello World”  Access characters using Index.  Eg: word1='Hello' word2='Hi' x=word1[1] print(x) y=word2[0] print(y) Character H e l l o W o r l D Index 0 1 2 3 4 5 6 7 8 9 10
  • 4.
     End ofthe string can be accessed using Negative Index.  Eg: “Hello World”  Eg: word=“Hello” x=word[-1] print(x) x=word[-5] print(x) Character H e l l o W o r l Negative Index -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 d
  • 5.
    Getting Length ofa String using len()  Built-in function  Obtain length of a string.  Eg: word="Python" l=len(word) print(l) Traversal through String with a loop  Extracting every character and performing some action is called Traversal.  It can be done using while or for loop
  • 6.
    1. Using forloop s="Python" for i in s: print(i, end=‘t’) 2. Using while loop s="Python" i=0 while i<len(s): #FORWARD TRAVERSAL print(s[i],end='t') i=i+1 s="Python" i=-1 while i>= -len(s): #BACKWARD TRAVERSAL print(s[i],end='t') i=i-1
  • 7.
    String Slices  Portionof a string is a string slice.  Extract a required number of characters using colon (:)  Syntax: st[i:j:k]  i is the first index or beginning index or start index  j is the last index or end index. If j index is not present, means slice should be till the end of the string.  k is the stride, to indicate no. of steps incremented. Default value is 1.  Eg: st=“Hello World” print(st[0:5:1])
  • 8.
    1. print (st[0:5]) 2.print (st[3:8]) 3. print (st[7:]) 4. print (st[::]) 5. print (st[::2]) 6. print (st[4:4]) 7. print (st[3:8:2]) 8. print (st[1:8:3]) 9. print (st[-4:-1]) 10. print (st[-1]) 11. print (st[:-1]) 12. print (st[:]) 13. print (st[::-1]) 14. print (st[::-2])
  • 9.
     Strings areimmutable [cannot change]  To modify string, create a new string.  Eg: 1. s="hello world“ #TypeError: 'str' object does not support item assignment s[3]='t‘ 2. s="hello world" s1=s[:3]+'t'+s[4:] #helto world print(s1) String are immutable
  • 10.
     Using loopswe can count the frequency of character.  Eg: w='Book' count=0 for letter in w: if letter=='o': count=count+1 print("The Occurences of Character 'o' is %d"%(count)) Looping and Counting
  • 11.
     Boolean operatortakes 2 string operands.  Returns True if 1st operand appears in 2nd Operand. Else False.  Eg: ‘el’ in ‘hello’ #True ‘x’ in ‘hello’ #False ‘EL’ in ‘Hello’ #False The in operator
  • 12.
     Comparison operatorslike: <,> and == applied to string objects.  Result in True or False  Comparisons happens using ACSII codes  Eg: 1. s='hello' if s=='hello': print('Same') #Same 2. s='hello' if s<='Hello': print('Lesser') else: print('Greater') #Greater String Comparison
  • 13.
     A-Z 65-90 a-z 97-122  0-9 48-57  Space 32  Enter 13