Home > Backend Development > Python Tutorial > How to cancel spaces in python output

How to cancel spaces in python output

爱喝马黛茶的安东尼
Release: 2019-06-26 11:50:20
Original
15493 people have browsed it

How to cancel spaces in python output

#How to remove spaces in python output? Here are several different methods:

1: strip() method, remove spaces at the beginning or end of the string

>>> a = " a b c "
>>> a.strip()
'a b c'
Copy after login

2: lstrip() method, remove characters Spaces at the beginning of the string

>>> a = " a b c "
>>> a.lstrip()
'a b c '
Copy after login

Related recommendations: "Python Video Tutorial"

3: rstrip() method, remove spaces at the end of the string

>>> a = " a b c "
>>> a.rstrip()
' a b c'
Copy after login

4: replace() method, you can remove all spaces

# replace主要用于字符串的替换replace(old, new, count)
>>> a = " a b c "
>>> a.replace(" ", "")
'abc'
Copy after login

5: join() method split() method, you can remove all spaces

# join为字符字符串合成传入一个字符串列表,split用于字符串分割可以按规则进行分割
>>> a = " a b c "
>>> b = a.split()  # 字符串按空格分割成列表
>>> b ['a', 'b', 'c']
>>> c = "".join(b) # 使用一个空字符串合成列表内容生成新的字符串
>>> c 'abc'
 
# 快捷用法
>>> a = " a b c "
>>> "".join(a.split())
'abc'
Copy after login

The above is the detailed content of How to cancel spaces in python output. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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