Home  >  Article  >  Backend Development  >  python data type --- string

python data type --- string

高洛峰
高洛峰Original
2016-12-02 16:56:471371browse

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

>>> name = "  Frank  "
>>> name.strip()
'Frank'

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

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

Merge join("connection of strings symbol ")

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

Judge whether a space is "in" in a substring

>>> name = "Frank Bain"
>>> '' in name
True
>>> "L" in name
False
>>>

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'
>>>

Judgment of string

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


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