Pytho基础(1)
占位符
%
text = "姓名:%s,年龄:%s。" %("json",29)
print(text)
>>> 姓名:json,年龄:29。
f-string
name = "张三"
age = 21
res = f"姓名:{name},年龄:{age}。"
print(res)
>>> 姓名:张三,年龄:21。
判断开头结尾
startswith
text = "Hello World"
print(text.startswith("He"))
>>> True
endswith
text = "Hello World"
print(text.endswith("ld"))
>>> True
替换
replace
text = "Hello World"
print(text.replace("or","ro"))
>>> Hello Wrold
大小写
upper
text = "Hello World"
print(text.upper())
>>> HELLO WORLD
lower
text = "Hello World"
print(text.lower())
>>>hello world
切割
split(从左到右)
text = "This is base Python."
print(text.split(" ", maxsplit=2))
>>> ['This', 'is', 'base Python.']
rsplit(从右到左)
text = "This is base Python."
print(text.rsplit(" ", maxsplit=2))
>>>['This is', 'base', 'Python.']
连接
join
t1 = ["This","is","base","Python."]
print(" - ".join(t1))
>>> This - is - base - Python.
去除空白
strip(只能去除两端的空白,中间的无法去除)
s1 = " world "
print(s1.strip())
>>>world
判断是否包含数字
isdecimal
n1 = "748392"
print(n1.isdecimal())
n2 = "634asjkqg"
print(n2.isdecimal())
>>>True
>>>False
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/114864.html