Truncating a string essentially means splitting a string into different substrings or extracting a substring from it. In Python, string operations are quite easy to execute and string truncating is no exceptional case. The good thing about working in python is that you can find packages and modules that you can import to solve your problem. However, for this guide, only built-in methods will be used.
Truncating String in Python Using Slicing
In Python, slicing is a method in which you define a “range” inside square brackets. When this range is used with a string, it subtracts a substring from the main string. Take a look at its syntax to know the usage of string slicing in Python
Syntax of String Slicing
In the above syntax:
- “string” is the string variable which will be truncated
- “startIndex” is the starting character index of the substring that will be created
- “endIndex” is the ending character index of the substring that will be created
Return Value
A substring truncated from the main string
Example of Truncating String with String Slicing
To demonstrate the working of the string slicing, create string variable:
Slice the string from the character index “0” to “5” and store the returned value in a new variable:
Finally, display the result:
Upon execution the terminal will display the following output:
To change a little bit, in the above code, only provide the starting index, leave the ending index as blank:
substring = string[4:]
print(substring)
Executing the code now, will produce the following output:
The output confirms that if the ending index is not provided then the substring goes to the end of the actual string. However, if the starting is not provided, then it starts the substring from the very first character:
substring = string[:10]
print(substring)
This code snippet produces the following result:
That was all about truncating string using the string slicing method.
Truncating Strings With the rsplit() Method
The second way of truncating a string in python is by using the rsplit() method. This method goes through the string and splits the string into parts upon encountering the “separator”, and the parts are stored in the form of a list. Look at the syntax given below to learn the usage of the rsplit() method in Python
Syntax of rsplit() Method
To explain the syntax:
- “string” is the string variable that will be truncated into parts
- “sepChar” defines the separator character, encountering which split the string into parts
- “limitItem” defines the maximum index of items to be stored in the list
Examples of Truncating String with rsplit() method
To demonstrate the working of the rsplit() method, take a string variable:
After that, apply the rsplit() method on the string variable, set the separator character equal to a blank space “ “ and store the result in a separate variable “truncParts”
After that, print the list variable “truncParts”:
This code produces the following output on the terminal:
The output confirms that the string has been split into parts at every occurrence of a “blank space”. Alternatively, to limit the number of total substrings that can be formed, use the following code:
truncParts = greetings.rsplit(" ",3)
print(truncParts)
Executing this code produces the following output:
From the output, it can be seen that the total number of items in the list is now “4”, and in the index, the maximum value is “3”.
That was all about truncating strings in Python!
Conclusion
To truncate a string in Python, the user can use the string slicing method and provide the starting and ending index of the substring in the square brackets. Alternatively, the user can also use the rsplit() method and create multiple substrings from the string by providing a “separator” character and then storing the substrings in the form of a list.
from https://ift.tt/MZLYRsu
0 Comments