Python字符串内建函数

字符串内建函数

- find函数

#find函数(查找and是否在string_example字符串中)
string_example = "hello world itheima and itheimaApp"
index = string_example.find("and")
print(index)
#若未找到则返回-1
index1 = string_example.find("ans")
print(index1)

运行结果:
Python字符串内建函数
- index函数

string_example = "hello world itheima and itheimaApp"
#判断hello是否在string_example字符串的0--5位置
index2 = string_example.index("hello",0,5)
print(index2)
#判断world是否在string_example字符串的0--5位置
index3 = string_example.index("world",0,5)
print(index3)

运行结果:
Python字符串内建函数

- count函数

  • count用于统计字符串里某个字符出现的次数
#count函数
#count用于统计字符串里某个字符出现的次数
string_example = "hello world itheima and itheimaApp"
#a = len(string_example)
#print(a)
result = string_example.count("i")
print(result)

运行结果:
Python字符串内建函数
- replace函数

  • replace函数把字符串中的old(旧的字符串)替换成new(新的字符串)
  • 参数说明:world:需要被替换掉的字符串;worlds:将world换成worlds;2:需要被替换的次数不超过2次
#replace函数把字符串中的old(旧的字符串)替换成new(新的字符串)
old_string = "hello world world itheima world and itheimaApp"
#参数说明:world:需要被替换掉的字符串;worlds:将world换成worlds;2:需要被替换的次数不超过2次
new_string =old_string.replace("world","worlds",2)

运行结果:
Python字符串内建函数

- split函数

  • 分隔符,默认为所有空字符,包括空格、换行(\n)、制表符(\t)等
    - 注意被分割的字符串将消失不见
#split函数
string_example = "hello world world itheima world and itheimaApp"
#分隔符,默认为所有空字符,包括空格、换行(\n)、制表符(\t)等
print(string_example.split())
#遇到i分割,分割的次数为1次(注意被分割的字符串将消失不见)
print(string_example.split("i",2))

运行结果:
Python字符串内建函数

- capitalize函数

  • 将字符串的第一个字母变成大写,其他字母变小写,该函数返回一个首字母大写的字符串
#capitalize函数
#将字符串的第一个字母变成大写,其他字母变小写,该函数返回一个首字母大写的字符串
string_example = "hello world WORLD itheima WORLD and itheimaApp"
print(string_example.capitalize())

运行结果:
Python字符串内建函数

- title函数

  • title函数返回“标题化”的字符串,也就是说所有的单词都是以大写开始,其余字母均为小写
#title函数
#title函数返回“标题化”的字符串,也就是说所有的单词都是以大写开始,其余字母均为小写
old_string = "hello world WORLD itheima WORLD and itheimaApp"
new_string = old_string.title()

运行结果:
Python字符串内建函数

- startswith函数

  • startswith函数用于检查字符串是否以指定子字符串开头。如果是,则返回True,否则返回False
#startswith函数
#startswith函数用于检查字符串是否以指定子字符串开头。如果是,则返回True,否则返回False
old_string = "hello world WORLD itheima WORLD and itheimaApp"
new_string = old_string.startswith("he")
print(new_string)

- endswith函数

  • endswith函数用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False
#endswith函数用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False
string_example = "hello world itheima and itheimaApp"
new_example = string_example.endswith("app")
new_example1 = string_example.endswith("App")
#由结果可得endswith区分大小写
print(new_example)
print(new_example1)

运行结果:
Python字符串内建函数

- upper函数

  • upper函数将字符串中的小写字母转换为大写字母
#upper函数将字符串中的小写字母转换为大写字母
old_string = "hello world itheima and itheimaApp"
new_string = old_string.upper()
print(new_string)

运行结果:
Python字符串内建函数

- ljust函数

  • 简单来说:左对齐,长度不过用括号中字符串代替
  • 标准含义:ljust函数返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度长度小于原字符串的长度,则返回原字符串
#ljust函数返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度长度小于原字符串的长度,则返回原字符串
string_exapmle = "hello world itheima and itheimaApp"
print(len(string_exapmle))
print(string_exapmle.ljust(33,"*"))
print(string_exapmle.ljust(34,"%"))
print(string_exapmle.ljust(35,"*"))

运行结果:
Python字符串内建函数

- rjust函数

  • 简单来说:右对齐,长度不过用括号中字符串代替
  • 标准含义: rjust函数返回一个原字符串右对齐,并使用空格填充至长度为width的新字符串。如果指定的长度小于字符串长度,则返回原字符串。
#rjust函数返回一个原字符串右对齐,并使用空格填充至长度为width的新字符串。如果指定的长度小于字符串长度,则返回原字符串。
string_exapmle = "hello world itheima and itheimaApp"
#string_example字符串长度
print(len(string_exapmle))
print(string_exapmle.rjust(33,"#"))
print(string_exapmle.rjust(34,"%"))
print(string_exapmle.rjust(50,"@"))

运行结果:
Python字符串内建函数

  • center函数
#center函数
#居中对齐,长度不够用括号中字符串代替
string_example = "hello world itheima and itheimaApp"
print(len(string_example))
print(string_example.center(30,"$"))
print(string_example.center(34,"$"))
print(string_example.center(50,"@"))
print(string_example.center(51,"!"))

运行结果:
Python字符串内建函数

- lstrip函数

  • lstrip函数用于截掉字符串左边的空格或指定的字符串,返回新的字符串
#lstrip函数
#lstrip函数用于截掉字符串左边的空格或指定的字符串,返回新的字符串
old_string_one = "        hello world itheima and itheimaApp"
old_string_two = "hello world itheima and itheimaApp"
#定义新的字符串
new_string_one = old_string_one.lstrip()
new_string_two = old_string_two.lstrip("hello")
#输出新定义的字符串
print(new_string_one)
print(new_string_two)

运行结果:
Python字符串内建函数