Home > Backend Development > Python Tutorial > python data type --- string

python data type --- string

高洛峰
Release: 2016-12-02 16:56:47
Original
1416 people have browsed it

Remove whitespace from strings, strip(), including spaces, tab keys, newlines

>>> name = "  Frank  "
>>> name.strip()
'Frank'
Copy after login

Split strings, split("separator"), group into a list

>>> name = "Apple, banbana, orice"
>>> name.split(",")
['Apple', ' banbana', ' orice']
>>>
Copy after login

Merge join("connection of strings symbol ")

>>> name = ['Frank', 'Marlon', 'Lee']
>>> "|".join(name)
'Frank|Marlon|Lee'
Copy after login

Judge whether a space is "in" in a substring

>>> name = "Frank Bain"
>>> '' in name
True
>>> "L" in name
False
>>>
Copy after login

Two string formatting and printing forms format

# 第一种表示方法
>>> msg = "Hello {name}, your age is {age}"
>>> msg.format(name="Frank", age=24)
'Hello Frank, your age is 24'
>>>

# 第二种表示方法 ,注意index 从0 开始
>>> msg = "Hello{0}, your age is {1}, sex is {2}"
>>> msg.format("Frank", 24, "man")
'HelloFrank, your age is 24, sex is man'
>>>
Copy after login

Judgment of string

>>> name = "Frank"
>>> name.upper()
'FRANK'
>>> name.lower()
'frank'
>>> name.startswith("F")
True
>>> name.endswith("k")
True
>>> name.endswith("a")
False
Copy after login


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