A String sequence
Len
Traversal with a for loop
String slices
Strings are immutable
Searching
Looping and counting
String methods
In operator
String comparisons
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
Strings in Python
1. Strings in Python
Vikram Neerugatti
Sri Venkateswara College of Engineering
01-04-2020
Vikram Neerugatti, Sri Venkateswara College of Engineering
andTechnology, Chittoor
1
2. Content
• A String sequence
• Len
• Traversal with a for loop
• String slices
• Strings are immutable
• Searching
• Looping and counting
• String methods
• In operator
• String comparisons
01-04-2020
Vikram Neerugatti, Sri Venkateswara College of Engineering
andTechnology, Chittoor
2
3. A Sequence types
• There are six sequence types:
• strings, “nani” or ‘c’
• Unicode strings,
• lists,
• tuples,
• buffers,
• and xrange objects.
01-04-2020
Vikram Neerugatti, Sri Venkateswara College of Engineering
andTechnology, Chittoor
3
4. Len
• It is a function to know the length
• It can be applied to any datatype or data structure
• For example:
• a="n"
• b="nani"
• print(a)
• print(b)
• d=len(a)
• g=len(b)
• x=[3,4,5]
• c=len(x)
• print(d,g,c)
01-04-2020
Vikram Neerugatti, Sri Venkateswara College of Engineering
andTechnology, Chittoor
4
5. Traversal with a for loop
Example:
b="nani is learnng python"
for v in b:
print(v)
01-04-2020
Vikram Neerugatti, Sri Venkateswara College of Engineering
andTechnology, Chittoor
5
6. String slices
• With the slicing we can create the substring
• Example:
• c="nani is good“
• print(c)
• d=c[5:7]
• print(d)
• e=c[8:12]
• print(e)
01-04-2020
Vikram Neerugatti, Sri Venkateswara College of Engineering
andTechnology, Chittoor
6
7. Strings are immutable
• Cant modify the strings once declared
• c="nani is good"
• print(c)
• d=c[6]
• print(d)
• #c[4]="7“
• but for lists, its possible(mutable)
• v=[5,6,7]
• print(v)
• v[2]=4
• print(v)
01-04-2020
Vikram Neerugatti, Sri Venkateswara College of Engineering
andTechnology, Chittoor
7
8. Searching
• To search a substring in a string is possible with find() method
• It returns the starting position of a substring in a sting and returnes -1 if not found.
• Example:
• c="nani is good"
• print(c)
• a=c.find("is")
• print(a)
• b=c.find("sonali")
• print(b)
01-04-2020
Vikram Neerugatti, Sri Venkateswara College of Engineering
andTechnology, Chittoor
8
9. Looping and counting
• Example:
• c="nani is good"
• total=0
• for i in c:
• total= total + 1
• print(total)
01-04-2020
Vikram Neerugatti, Sri Venkateswara College of Engineering andTechnology,
Chittoor 9
13. Summary
• A String sequence
• Len
• Traversal with a for loop
• String slices
• Strings are immutable
• Searching
• Looping and counting
• String methods
• In operator
• String comparisons
01-04-2020
Vikram Neerugatti, Sri Venkateswara College of Engineering andTechnology,
Chittoor 14