Home > Backend Development > Python Tutorial > Python program: Trim string from right side of string

Python program: Trim string from right side of string

王林
Release: 2023-09-08 18:21:08
forward
1391 people have browsed it

Python program: Trim string from right side of string

In Python, we have a predefined function rstrip() to remove the characters on the right side. This means it will remove spaces on the right side of the string.

Let us take an example to understand how to trim from the left side of a string.

  • Remove the right string LESS from the given string "WIRELESS" and get the resulting value as "WIRE".

  • In the given string "kingdom", delete the string dom on the right side, and the result value is "king".

grammar

The syntax used in the following examples is −

isspace()
Copy after login

This is a predefined method in Python for allowing whitespace, newlines, or spaces in characters.

rstrip("parameter as a string")
Copy after login

This is a predefined method used in Python that accepts a character as argument and removes that character from the right side of the string.

endswith()
Copy after login

This is a built-in method in Python that returns true if the string ends with a specific value.

Example 1

In this program, we store the input string in the variable ‘str’. The variable 'i' is then initialized to the value 5, and characters after the 5th index will be trimmed later. Next, the variable ‘str’ is iterated over the variable ‘char’ using a for loop. Then use the if statement to search for spaces using the isspace() method. If a space is not found in the string, it breaks the loop and the variable "i" is decremented for each white space character. Now we trim the characters using str[:i] and store the value in variable 'trim_str'. Finally, we use the variable 'trim_str' to print the result.

#trim the string from the right
str = "UNIVERSITY"
i = 5
for char in str:
   if not char.isspace():
      break
   i -= 1
trim_str = str[:i] #The use before slicing removes the right string.
print("Trim the string of", i," characters from right:", trim_str)
Copy after login

Output

Trim the string of 5 characters from right: UNIVE 
Copy after login

Example 2

In this program, we store the input string in the variable ‘my_str’. We then remove the character "a" from the right side of the string and store it in the variable 'trim_str'. Finally, we use the variable 'trim_str' to print the result.

#Trim the string from right
my_str = "aaaaa!King!aaaaa"
trim_str = my_str.rstrip("a")
print(trim_str)
Copy after login

Output

aaaaa!King!
Copy after login
The Chinese translation of

Example 3

is:

Example 3

In this program, we will store the input string in the variable str_name. The correct deletion string is then stored in the variable del_suffix. Then use an if statement to check the condition for removing the right side of the string using the built-in method endswith(). Next, use the replace() method to remove the given string and store it in the variable str_name. Finally, we use the str_name variable to print the output.

str_name = "abcdefghi"
del_suffix = "ghi"
if str_name.endswith(del_suffix):
   str_name = str_name.replace(del_suffix, "")
print("After deleting the suffix from left side:",str_name)
Copy after login

Output

After deleting the suffix from left side: abcdef
Copy after login
The Chinese translation of

Example 4

is:

Example 4

In the following program, we store the input string in the variable s. Then use the built-in method removesuffix() to set a string named 'iop', remove the string from the right and in the print() function.

s = 'qwertyuiop'
print(s.removesuffix('iop'))
Copy after login

Output

qwertyu
Copy after login

in conclusion

We understand the difference between these two examples by trimming the string from the left. We see several different methods used in the examples, including isspace(), rstrip(), endswith(), and slicing techniques. Slicing techniques are often used to trim strings from the right side.

The above is the detailed content of Python program: Trim string from right side of string. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template