Updated April 15, 2023
Introduction to Python string manipulation
In Python, the string is declared using either single or double quotes, which it defines as a sequence of characters. String manipulation is a process of manipulating the string, such as slicing, parsing, analyzing, etc. In many different programming languages, including Python, provides string data type to work with such string manipulating, which uses different functions of the string provided by string data type “str” in Python. In Python, the string data type is used for representing textual data, where it is used in every application that involves strings. In this article, we will discuss various functions of the string used in string manipulation. In this topic, we are going to learn about Python string manipulation.
Working of String manipulation in Python
In Python, string class “str” provides various methods for string manipulation where almost every application uses string in their application. In Python, strings are either declared using single or double quotes, unlike other programming languages that require data types to declare any other type. In Python, some built-in string methods are used for string manipulation. In Python, there are operators, functions, and methods which are used in string manipulation. Let us see one by one in the below section.
Operators used in string manipulation
First, let us use a few string operators that can be used in string manipulation.
1. * operator
This operator is used when we want to create multiple copies of the given string. Let us consider an example below to demonstrate this operator.
star_op = "educba " res = star_op * 4 print(res)
In this above program, we can see that we have declared a string using the “star_op” variable with “educba” string, and using the * operator, we have “res” variable which will store the result of the statement “res * 4” which will print the given string “educba” 4 times.
2. + operator
Python + operator can be used to concatenate the given strings and can be demonstrated in the below example.
str1 = "Educba " str2 = "Training " str3 = "Institute" res = str1 + str2 + str3 print(res)
In the above program, we can see we are using the “+” operator for concatenating the given string, and the result is printed as shown in the above screenshot. Similarly, many different operators are used for string manipulation, such as “in”, “not”,, etc. operators.
Now we will see a few string built-in functions in Python that is used for string manipulation. Let us discuss a few string built-in functions in the below section. There are many different functions such as chr(), len(), str() and ord().
Let us demonstrate these functions with an example for each function.
print("Python program for demonstrating string built-in function for string manipulation") print("\n") str1 = "E" str2 = 35 str3 = "Educba Training Institute" res1 = ord(str1) res2 = chr(str2) res3 = len(str3) print("This str() function is used for returning a string representation of an object") print(str(45.9)) print("\n") print("The ord() function is used for converting given character to an integer") print(res1) print("\n") print("The chr() function is used for converting given integer to character") print(res2) print("\n") print("The len() function is used to get the length of a given string") print(res3) print("\n")
In the above program, we can see that we have used 4 different string built-in functions to demonstrate the string manipulation using these string functions. In the above program, we can see the str() function will return the object that is passed to this function in string representation, ord() function, which will return the integer form of the given character, chr() function for returning the character form for the given integer passed to the function, len() function returns the number of characters in the given string as the argument.
String indexing is another process for string manipulation in Python. As a string has a set of characters to access each character of the string, we need to use the indexing method to retrieve the specified character using the index or key values. Let us demonstrate in the below section with an example.
Suppose let us declare a string and give an index value to each character of the given string.
str_indx = "Educba" print("The character of the specified index of 2 of the given string is as follows") print(str_indx)
In the above program, we can see we declared a string “Educba”, and each has an index as E as 0, d as 1, u as 2, c as 3, b as 4, a as 5. There we have asked to print the character with index 2, which is “u”.
String slicing is another process used in string manipulation. String slicing in Python is defined as the extraction of a substring from the given string, where Python allows a form of indexing which will help in string slicing. String slicing uses the form s[a:b] where “a” is the start index, and “b” is the end index which returns the string from index “a” to index “b”. Let us demonstrate this in the below example.
str_indx = "Educba" print("The given string is sliced to obtain substring is as follows") print(str_indx[2 : 5])
In the above program, we can see we declared a string “Educba”, and we are trying to extract a substring or part of the given string using indexes specified for each of the characters. In the program, we have specified to extract string str_indx[2:5], which will result in the “ucb” substring. Therefore we will be extracting characters from index 2 to index 5 to form a substring which we can see in the above screenshot.
In this article, we can conclude that string manipulation in Python is defined as a process of manipulating string using processes like parsing, slicing, indexing, etc. In this article, we saw string manipulation is done using string operators, functions, and methods provided by Python. Therefore we saw few string operators, string functions, and string methods are described with examples. We also saw string indexing and string slicing concepts with examples for string manipulation.
This is a guide to Python string manipulation. Here we discuss the Working of String manipulation in Python along with the Operators used in string manipulation. You may also have a look at the following articles to learn more –