python字符串

1.字符串函数

name = "james jacKSON"
name.title()   # 字符串首字母大写,其余字母变小写
name.upper()
name.lower()

2.在字符串中使用变量 - f字符串 (Python3.6引入的)

first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name} {last_name}"
print(full_name)

print(f"Hello, {full_name.title()}")

message = f"Hello, {full_name.title()}"
print(message)

3.制表符和换行符来添加空白

print("Language:\n\tPython\n\tC\n\tJavaScript")

4.删除空白

# 末尾去掉空格
favorite_language.rstrip()
# 开头去掉空格
favorite_language.lstrip()
# 两头去掉空格
favorite_language.strip()

5.字符串切割,替换,截取?

5.1 是否包含子串

if '351294' in '3.141592653512948375229873453922':
    print("yes")
else:
    print("no")

5.2切割split()

# 默认以空格切分,返回一个列表
title= "Alice \t  in W.onderland"
title.split()
posted @ 2023-07-05 18:38  钱塘江畔  阅读(11)  评论(0)    收藏  举报