Python基础第四篇(字符串)

一、字符串输出

# 一、字符串输出
str1 = '0123456789'
print(str1)      # abcdef

二、字符串切片–截取部分数据

str1[开始位置下标: 结束位置下标: 步长]

# 二、字符串切片--截取部分数据
# 字符串切片--截取部分数据
# str1[开始位置下标: 结束位置下标: 步长]
print(str1[1])      # 1
print(str1[2:5:1])  # 234
print(str1[2:5:2])  # 24
print(str1[:5])     # 01234
print(str1[:])      # 0123456789

# 负数测试
print(str1[::-1])      # 9876543210
print(str1[-4:-1])     # 678
print(str1[-4:-1:1])   # 678
print(str1[-4:-1:-1])  # 无输出,注意选取方向
print(str1[-1:-4:-1])  # 987

三、字符串检测函数

  1. find()
    语法: 字符串序列.find(子串, 开始位置下标, 结束位置下标)
    语意:从左往右检测某个子串是否包含在这个字符串,在则返回子串开始位置的下标,否则返回-1
    rfind() 功能和 find() 相同,但查找方向为右侧开始
  2. index()
    语法: 字符串序列.index(子串, 开始位置下标, 结束位置下标)
    语意:检测某个子串是否包含在这个字符串中,在则返回子串开始位置的下标,否则报异常
    rindex() 功能和 index() 相同,但查找方向为右侧开始
  3. count()
    语法: 字符串序列.count(子串, 开始位置下标, 结束位置下标)
    语意:返回某个子串在字符串中出现的次数
# 三、字符串检测函数
# 1. find()
# 语法: 字符串序列.find(子串, 开始位置下标, 结束位置下标)
# 从左往右检测某个子串是否包含在这个字符串
# 在-返回子串开始位置的下标,否则返回-1
# rfind()
# 功能和find()相同,但查找方向为右侧开始
str1 = 'hello python'
print(str1.find('python'))      # 6
print(str1.find('lo ', 0, 9))   # 3

# 2. index()
# 语法: 字符串序列.index(子串, 开始位置下标, 结束位置下标)
# 检测某个子串是否包含在这个字符串中
# 在-返回子串开始位置的下标,否则报异常
# rindex()
# 功能和index()相同,但查找方向为右侧开始
str1 = 'hello python'
print(str1.index('python'))     # 6
print(str1.index('lo ', 0, 9))  # 3

# 3. count()
# 语法: 字符串序列.count(子串, 开始位置下标, 结束位置下标)
# 返回某个子串在字符串中出现的次数
str1 = 'hello python'
print(str1.count('python'))     # 1
print(str1.count('l', 0, 9))    # 2
print(str1.count('l', 2, 9))    # 2
print(str1.count('l', 3, 9))    # 1
print(str1.count('l', 4, 9))    # 0

四、字符串修改函数

  1. replace()
    语法: 字符串序列.replace(旧子串, 新子串, 替换次数)
    语意:替换, 原字符串不变
  2. split()
    语法: 字符串序列.split(分割字符, num)
    num 表示分割字符出现的次数,返回数据的个数为 num + 1
    语意:分割
  3. join()
    语法:字符或子串.join(多字符串组成的序列)
    语意:用一个字符或子串合并字符串,即将多个字符串合并为一个新的字符串
# 四、字符串修改函数
# 1. replace()
# 语法: 字符串序列.replace(旧子串, 新子串, 替换次数)
# 替换, 原字符串不变
str1 = 'hello python, i love python'
print(str1)
new_str = str1.replace('python', 'world ', 1)
print(str1)
print(new_str)
new_str = str1.replace('python', 'world ', 2)
print(str1)
print(new_str)

# 2. split()
# 语法: 字符串序列.split(分割字符, num)
# num 表示分割字符出现的次数,返回数据的个数为 num + 1
# 分割
str1 = 'hello python, i love python world'
print(str1)
print(str1.split('python', 2))

# 3. join()
# 语法: 字符或子串.join(多字符串组成的序列)
# 用一个字符或子串合并字符串,即将多个字符串合并为一个新的字符串
list1 = ['hi', 'python', 'i', 'love', 'you']
t1 = ('aa', 'bb', 'cc', 'dd')
print('_'.join(list1))
print('...'.join(t1))

五、字符串字母大小写转换函数

  1. capitalize()
    语意:将字符首字母转换成大写
  2. title()
    语意:将字符串每个单词首字母转换成大写
  3. upper()
    语意:将字符全部转换成大写
  4. lower()
    语意:将字符串中大写转换成小写
# 五、字符串字母大小写转换函数
# 1. capitalize()
# 将字符首字母转换成大写
str1 = 'hello world and python'
new_str = str1.capitalize()
print(str1)
print('capitalize:', new_str)

# 2. title()
# # 将字符串每个单词首字母转换成大写
str1 = 'hello world and python'
new_str = str1.title()
print(str1)
print('title:', new_str)

# 3. upper()
# 将字符全部转换成大写
str1 = 'hello world and python'
new_str = str1.upper()
print(str1)
print('upper:', new_str)

# 4. lower()
# 将字符串中大写转换成小写
str1 = 'HELLO WORLD AND PYTHON'
new_str = str1.lower()
print(str1)
print('lower:', new_str)

六、字符串空白删除函数

  1. lstrip()
    语意:删除字符串左侧空白字符
  2. rstrip()
    语意:删除字符串右侧空白字符
  3. strip()
    语意:删除字符串两侧空白字符
# 六、字符串空白删除函数
# 1. lstrip()
# 删除字符串左侧空白字符
str1 = "   hello world and python    "
new_str = str1.lstrip()
print(str1)
print('lstrip:', new_str)

# 2. rstrip()
# 删除字符串右侧空白字符
str1 = "   hello world and python     "
new_str = str1.rstrip()
print(str1)
print('rstrip:', new_str)

# 3. strip()
# 删除字符串两侧空白字符
str1 = "   hello world and python     "
new_str = str1.strip()
print(str1)
print('strip:', new_str)

七、字符串对齐函数

  1. ljust()
    语法: 字符串序列.ljust(长度, 填充字符)
    语意:字符串对齐
# 七、字符串对齐
# 1. ljust()
# 语法: 字符串序列.ljust(长度, 填充字符)
# 字符串对齐
str1 = "hello"
print(str1.ljust(12))

八、字符串判断函数

  1. startswith()
    语法: 字符串序列.startswith(子串, 开始位置下标, 结束位置下标)
    语意:判断字符串是否以某个子串开头,返回bool类型

  2. endswith()
    语法: 字符串序列.endswith(子串, 开始位置下标, 结束位置下标)
    语意:判断字符串是否以某个子串结尾,返回bool类型

  3. isalpha()
    语意:如果字符串至少有一个字符并且所有字符都是字母则返回True, 否则返回False

  4. isdigit()
    语意:如果字符串只包含数字则返回True, 否则返回False

  5. isalnum()
    语意:如果字符串至少有一个字符并且所有字符都是字母或数字则返回True, 否则返回False

  6. isspace()
    语意:如果字符串只包含空白,则返回True, 否则返回False

# 八、字符串判断
# 1. startswith()
# 语法: 字符串序列.startswith(子串, 开始位置下标, 结束位置下标)
# 判断字符串是否以某个子串开头
str1 = "hello world and python"
print(str1.startswith('hello', 0, 20))  # True
print(str1.startswith('python', 0, 20)) # False

# 2. endswith()
# 语法: 字符串序列.endswith(子串, 开始位置下标, 结束位置下标)
# 判断字符串是否以某个子串结尾
str1 = "hello world and python"
print(str1.endswith('hello', 0, 20))    # False
print(str1.endswith('python', 0, 22))   # True

# 3. isalpha()
# 如果字符串至少有一个字符并且所有字符都是字母则返回True, 否则返回False
str1 = 'hello'
str2 = 'hello123456'
print(str1.isalpha())   # True
print(str2.isalpha())   # False

# 4. isdigit()
# 如果字符串只包含数字则返回True, 否则返回False
str1 = 'hello'
str2 = 'hello123456'
str3 = '123456'
print(str1.isdigit())   # False
print(str2.isdigit())   # False
print(str3.isdigit())   # True

# 5. isalnum()
# 如果字符串至少有一个字符并且所有字符都是字母或数字则返回True, 否则返回False
str1 = 'hello'
str2 = 'hello123456'
str3 = '123456'
str4 = '123456---'
print(str1.isalnum())   # True
print(str2.isalnum())   # True
print(str3.isalnum())   # True
print(str4.isalnum())   # False

# 6. isspace()
# 如果字符串只包含空白,则返回True, 否则返回False
str1 = 'he   llo'
str2 = 'hello123456'
str3 = '123456'
str4 = '  '
print(str1.isspace())   # True
print(str2.isspace())   # True
print(str3.isspace())   # True
print(str4.isspace())   # False
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值