python
复制
s = "hello"
print(s.islower()) # 输出: True
s = "Hello"
print(s.islower()) # 输出: False
20. str.isupper()
功能: 检查字符串中的所有字母字符是否都是大写。
示例:
python
复制
s = "HELLO"
print(s.isupper()) # 输出: True
s = "Hello"
print(s.isupper()) # 输出: False
21. str.format()
功能: 格式化字符串,将占位符替换为指定的值。
示例:
python
复制
name = "Alice"
age = 25
print("My name is {} and I am {} years old.".format(name, age))
# 输出: My name is Alice and I am 25 years old.
22. f-string(Python 3.6+)
功能: 使用 f 前缀的字符串格式化方法。
示例:
python
复制
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
# 输出: My name is Alice and I am 25 years old.
23. str.encode()
功能: 将字符串编码为字节对象。