For example, we have a string “My World is very unique,” and we only need “My world” from the string, so we just truncate the string to “My world” using python functions. Another example is, suppose we have a string “abcxyz” and we only need the “abc” part of the string, so we will truncate the string and extract the “abc” characters from the string.
In this tutorial, you will be able to learn the different techniques to truncate strings with the help of simple and easy examples. So, let us have a look at the example codes and learn to truncate strings through basic python methods.
The first method we are going to discuss is truncating string using the string slice method. The string slice method extracts the specific words or characters from within the string. It takes the starting and ending point of the word or character that needs to be extracted from the string.
The syntax of string slicing is [a:b], where ‘a’ index refers to the starting point of the string, and ‘b’ index refers to the last point of the string, which is the point up until you want to truncate the string. The ‘b’ index value can be equal to the length of the original string, or it can be less than the length of the original string. If you do not provide the ‘a’ index, the default starting point will be 0, and the same is the case with the ‘b’ index; if the ‘b’ index is not provided, the default ending point is the length of the original string, and whole string will be returned without truncating. Below, you can find the code to truncate strings using the string slicing method.
stringTrun = _string[0:14]
The above given code will truncate the string from 0’th character to the 14th character. Have a look at the output of the above code.
The other way to truncate a string is to use a rsplit() python function. rsplit() function takes the string, a delimiter value to split the string into parts, and it returns a list of words contained in the string split by the provided delimiter. However, if you just need one word or just one character, you can provide the length at which the word or the character exists in the string.
The syntax of rsplit() is rsplit(delimiter)[length to truncate]. The ‘delimiter’ is the separator value based on which the string will be divided into parts. The ‘length to truncate’ is the number at which the word exists in the string. If you provide the ‘length to truncate’ in the form of string slicing, it will return the number of words provided as ‘length to truncate’. Though the ‘length to truncate’ is optional, means if you do not provide it, the rsplit() function will return the whole string as a list of words separated by the delimiter. Here is a sample code to truncate string using rsplit() function.
stringTrun = _string.rsplit(" ")
The above code will split the string on the delimiter “ “ and return the 0th character as ‘length to truncate’ is 0. After when you dry run the code, the following output will be generated and you will get the following output.
Our third example is very simplest and easiest as it uses the python built-in shorten function by importing the textwrap library. The syntax for textwrap.shorten function is textrwrap.shorten(string,width,placeholder). String refers to the string that needs to be truncated, and width represents the number up until you need to truncate the string, and the placeholder is the characters which are appended at the end of the string once it is truncated. Here is a sample code for the textwrap.shorten() function.
str = textwrap.shorten('I love python language', width=15, placeholder='.')
The above code will generate the following output:
This tutorial was about how to truncate a string in Python. Different ways of truncating a string using the Python programming language are discussed. The first method we used is string slicing, and the second method is rsplit() python function. The rsplit() and string slicing can be used in combination and get the desired result as per the user’s requirements. Lastly, we explained the built-in function of Python textwrap.shorten.