Home  >  Article  >  Backend Development  >  How to remove the last space in Python

How to remove the last space in Python

(*-*)浩
(*-*)浩Original
2019-07-03 11:02:2611565browse

strip() function removes spaces\n\r\tUsage of function

How to remove the last space in Python

strip removes spaces on both sides at the same time(recommended learning: Python video tutorial)

lstrip Remove the spaces on the left

rstrip Remove the spaces on the right

Specific examples are as follows:

>>>a=" hello world!!  "
>>>a.lstrip() 'hello world!! '
>>>a.rstrip() ' hello world!!'
>>>a.strip() 'hello world!!'

Statement: s is a string, rm is the character sequence to be deleted

s.strip(rm) deletes the s string The characters in the rm deletion sequence at the beginning and end of the s string

s.lstrip(rm) Delete the characters in the rm deletion sequence at the beginning and end of the s string

s.rstrip(rm) Delete the characters in the rm deletion sequence at the end of the s string

Note:

When rm is empty, whitespace characters (including '\n') are deleted by default , '\r', '\t', ' ')

>>> a = ' hello world!!'
>>> a.strip()
'hello world!!'
>>> a='\t\thello world!!'
'hello world!!'
>>> a = 'hello world!!\r\n'
>>> a.strip()
'hello world!!'

For more Python-related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of How to remove the last space in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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