Lecture 7 - Strings in Python With Examples - 1
Lecture 7 - Strings in Python With Examples - 1
In this article, I will discuss Strings in Python with Examples. Please read
our previous article discussing Looping Statements in Python with
examples. You will understand the following pointers in detail at the end
of this article.
1. What is a string in Python?
2. What is Slicing in Python?
3. What is Immutable in Python?
4. Strings are immutable in Python
5. Mathematical operators on string objects in Python
6. How do you find the length of a string in Python?
7. How to remove spaces from a string in Python?
8. How do you Find substrings in a string in Python?
9. How do you Count substrings in a given String in Python?
10. How to replace a string with another string in Python?
11. Does the replace() method modify the string objects
in Python?
12. How to Split a string in Python?
13. How to Join strings in Python?
14. How do you change cases of string in Python?
15. How to Formatting the Strings in Python?
Reminder
We have already learned the first Hello World program in Python. In that
program, we print a group of characters using the print() function. Those
groups of characters are called a string.
for embedding single quote text in a string the string must be in double
quote
s2 = 'Welcome to "python" learning'
for embedding double quote text in a string the string must be in single
quote
for i in range(len(s1)):
print(i,' element is = ',s1[i],' id = ', id(s1[i]))
Output
0 element is = p id = 140731012446792
1 element is = a id = 140731012445952
2 element is = u id = 140731012447072
3 element is = r id = 140731012446904
4 element is = i id = 140731012446400
5 element is = id = 140731012442312
6 element is = g id = 140731012446288
7 element is = a id = 140731012445952
8 element is = r id = 140731012446904
9 element is = h id = 140731012446344
10 element is = w id = 140731012447184
11 element is = a id = 140731012445952
12 element is = l id = 140731012446568
# 01234567890
s1 = 'Ashish Negi'
print(s1[1])
for i in range(len(s1)):
print(s1[i], end = " ")
print()
for i in range(len(s1)):
print(i, end = " ")
Output :
Ashish Negi
0 1 2 3 4 5 6 7 8 9 10
# 0123456789012
s1 = "pauri garhwal"
# 3210987654321 negative index
for i in range(len(s1)):
print(i,' element is = ',s1[i],' id = ', id(s1[i]))
for i in range(-1,(-1)*(len(s1)+1),-1 ):
print(i,' element is = ',s1[i])
Output :
0 element is = p id = 140731012446792
1 element is = a id = 140731012445952
2 element is = u id = 140731012447072
3 element is = r id = 140731012446904
4 element is = i id = 140731012446400
5 element is = id = 140731012442312
6 element is = g id = 140731012446288
7 element is = a id = 140731012445952
8 element is = r id = 140731012446904
9 element is = h id = 140731012446344
10 element is = w id = 140731012447184
11 element is = a id = 140731012445952
12 element is = l id = 140731012446568
-1 element is = l
-2 element is = a
-3 element is = w
-4 element is = h
-5 element is = r
-6 element is = a
-7 element is = g
-8 element is =
-9 element is = i
-10 element is = r
-11 element is = u
-12 element is = a
-13 element is = p
Example:
Different Cases:
wish = “Hello World”
1. wish [::] => accessing from 0th to last
2. wish [:] => accessing from 0th to last
3. wish [0:9:1] => accessing string from 0th to 8th means (9-1)
element.
4. wish [0:9:2] => accessing string from 0th to 8th means (9-1)
element.
5. wish [2:4:1] => accessing from 2nd to 3rd characters.
6. wish [:: 2] => accessing entire in steps of 2
7. wish [2 ::] => accessing from str[2] to ending
8. wish [:4:] => accessing from 0th to 3 in steps of 1
9. wish [-4: -1] => access -4 to -1
Note:
• If you are not specifying the beginning index, it will consider
the beginning of the string.
• If you are not specifying the end index, it will consider the
end of the string.
• The default value for the step is 1
Example: Slicing operator using several use cases (Demo11.py)
wish = "Hello World"
print(wish[::])
print(wish[:])
print(wish[0:9:1])
print(wish[0:9:2])
print(wish[2:4:1])
print(wish[::2])
print(wish[2::])
print(wish[:4:])
print(wish[-4:-1])
Output:
Once we create an object, then the state of the existing object can be
changed or modified. This behavior is called mutability.