String manipulation in Python is an important topic for python programming or computer science class 11 and Informatics Practices class 11.
Topics Covered
Introduction to String Manipulation in Python
As you are aware that python allows to use following built-in data types:
- Numbers
- Strings
- List
- Tuple
- Dictionary
So you have basic ideas about strings. A string data type is representing text values in the program. If you are accepting anything in single or double or triple quotes in python is known as a string.
It can be set of letters, symbols or numbers also.
Moreover you are also familiar with input and output operations as well. So let’s explore the string manipulation in python with some more operators and functions.
Accessing a string through loop
As we have discussed a string is a collection of letters, numbers, and symbols each entity has its own position in the string. This is called an index. So using by using these indices you can access the string trough a loop.
Accessing a string by its index letter by letter is also known as traversing.
Traversing or Accessing string through for loop
To access the string letter by letter for loop can be used. This process is also known as traversing. The index used in this traversing is known as forward indexing. Take a look at the following code:
>>> text = "TutorialAICSIP"
>>> for ch in text:
print(ch,end="*")
The output will be:
T*u*t*o*r*i*a*l*A*I*C*S*I*P*
Traversing or Accessing string through the loop using the range function
text = "TutorialAICSIP"
>>> for ch in range(0,len(text),1):
print(text[ch],end="*")
The output will be:
T*u*t*o*r*i*a*l*A*I*C*S*I*P*
When you want to access the string using range function another built-in function len() will be used to identify the length of a string. The index used in the reversed traversing is known as backward indexing.
Reverse a string
>>> text ="TutorialAICSIP"
>>> l =len(text)
>>> for ch in range(-1,(-l-1),-1):
print(text[ch],end="")
The output will be:
PISCIAlairotuT
String Manipulation in Python using Basic Operators
As you have used basic arithmetic operators + and * for “addition” and “multiplication” respectively.
These operators can be used for string manipulation in python as well. Here in string manipulation these operators are known as “concatenation” and “replication” operator.
String concatenation operator “+”
It is used to join two words. Python strings are immutable in nature. Hence every time when any such operations performed on strings, python will create a new string instead of modifying older one. Observe the below given code and output:
>>> text ="Tutorial" + "AI-CS-IP"
>>> print(text)
The output will be:
TutorialAI-CS-IP
Valid combinations for + operator:
- n + n = 3 + 3 = 6
- s + s = “3” + “3” = 33
In python, the + operator works with numbers and strings separately for addition and concatenation, but in same statement it can’t be combined with a number + string and vice versa. See these combinations:
Invalid combinations:
- n + s = 3 + “3”
- s + n = “3” + 3
String replication operator “*”
When ‘*’ is used with a number and string, It is used to replicate or repeat the string number of time as specified in the expression. You can use number * string or string * number as well.
>>> text ="TutorialAICSIP"
>>> print(2*text,"-->",text*2)
The output will be:
TutorialAICSIPTutorialAICSIP --> TutorialAICSIPTutorialAICSIP
Valid combination for * operator:
- n * n = 3 * 3 = 9
- s * n = “$” * 3 = “$$$”
- n * s = 3 * “$” = “$$$”
Invalid combinations:
- s * s = “3” * “3”
Some facts:
- Numbers combination returns the multiplication.
- String combination will error in “*” operator.
Note: This operator can operate either string or number only, not both together.
String Manipulation in Python using Membership Operators
There are two membership operators supported for string manipulation in python.
- in
- not in
The “in” operator
As you have already used “in” operator with for loop. It us used to check the specified values is available there in specified string or not. It returns True if the text is found otherwise returns False.
text ="TutorialAICSIP"
if "Tutor" in text:
print("Tutor found")
else:
print("Tutor not found")
The output will be:
Tutor found
The not in Operator
It is used reversed than “in” operator. Returns True when specified text is not available in the text, other wise False.
text ="TutorialAICSIP"
if "Tutor" not in text:
print("Tutor not found")
else:
print("Tutor found")
The output will be:
Tutor not found
String Manipulation in Python using comparison Operators
Read more about comparison operators form here.
Using ASCII values Python code
Click here to know about ASCII values.
Python provides ord() function to know the ASCII value of the specified character.
Ex.: ord(‘D’) returns 68
If you want to know the character from ASCII code then python provides chr() function.
Ex. chr(98)returns ‘b’
Slices in String
Slices is another advanced feature supported by python. It will return the specific part from the given string and ranges. It will take the form of str[n1:n2].
>>> text="TutorialAICSIP"
>>> print(text[0:5])
Tutor
>>> print(text[3:9])
orialA
>>> print(text[:5])
Tutor
>>> print(text[5:])
ialAICSIP
>>> print(text[-1:])
P
>>> print(text[:-1])
TutorialAICSI
>>> print(text[-5:-4])
I
>>> print(text[1:3:3])
u
Follow this link for string functions notes:
Click here to read about python string functions.
Click here to read the common string operations from python docs.
Click here to read the documentation of python string methods.
Watch this video for more understanding:
Thank you for reading this article. Feel free to share your views, feedback, or doubts regarding this article in the comment section. Share this article with your friends.