String manipulation in Python is an important topic for python programming or computer science class 11 and Informatics Practices class 11.
Introduction to String Manipulation in Python
As you are aware that python allows to use following built-in data types:
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.
Accessing string through for loop
To access the string letter by letter for loop can be used. 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*
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.
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. 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
- n + s = 3 + “3”
- s + n = “3” + 3
String replication operator “*”
It is used to replicate 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 * “$” = “$$$”
- s * s = “3” * “3”
- 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.
- 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 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
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.
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.