字符串的基本使用
字符串的操作
- 在python交互界面定义一个字符串,例如:
hello_str = ""
- 输入
hello_str.
按下TAB键,交互界面就会提供字符串的所有操作方法
字符串操作演练-1
space_str = " \t\n\r"
print(space_str.isspace())
num_str = "1"
print(num_str.isdecimal())
print(num_str.isdigit())
print(num_str.isnumeric())
hello_str = "hello world"
print(hello_str.startswith("hello"))
print(hello_str.endswith("world"))
print(hello_str.find("llo"))
print(hello_str.find("abc"))
hello_str.replace("world","python")
print(hello_str)
字符串操作演练-2
poem = ["登鹳雀楼",
"王之涣",
"白日依山尽",
"黄河入海楼",
"欲穷千里目",
"更上一层楼"]
for poem_str in poem:
print("|%s|" % poem_str.center(10," "))
字符串操作演练-3
poem = ["\t\n登鹳雀楼",
"王之涣",
"白日依山尽\t\n",
"黄河入海楼",
"欲穷千里目",
"更上一层楼"]
for poem_str in poem:
print("|%s|" % poem_str.strip().center(10," "))
字符串操作演练-4
poem_str = "登鹳雀楼\t 王之涣 \t 白日依山尽 \t \n 黄河入海楼 \t\t 欲穷千里目 \n 更上一层楼"
print(poem_str)
poem_list = poem_str.split()
print(poem_list)
result = " ".join(poem_list)
print(result)