The document discusses various string manipulation techniques in Python such as getting the length of a string, traversing strings using loops, slicing strings, immutable nature of strings, using the 'in' operator to check for substrings, and comparing strings. Key string manipulation techniques covered include getting the length of a string using len(), extracting characters using indexes and slices, traversing strings with for and while loops, checking for substrings with the 'in' operator, and comparing strings.
2. 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
3. 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
4. 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
5. 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
6. 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
7. 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])
9. 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
10. 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
11. 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
12. 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
13. A-Z 65-90
a-z 97-122
0-9 48-57
Space 32
Enter 13