Python学习的第十天:字符串的使用方法

Python学习的第十天

评价算法好坏的标准:渐近时间复杂度和渐近空间复杂度

渐近时间复杂度通过O标记

os代表操作系统

os.windows(‘clear’) 用于清除输出

time.sleep() 用于让程序休眠

字符串

字符串的运算
  1. 拼接 +
contents = [
    '请不要相信我的美丽',
    '更不要相信我的爱情',
    '因为在涂满油彩的面孔下',
    '有着一颗戏子的心'
]
# 将列表中的元素用指定的字符串连接起来
print(' '.join(contents))

在这里插入图片描述

  1. 重复 *

  2. 成员运算in/not in

  3. 比较 从首位到末位按照字符的编码大小进行比较(不清楚对应编码可以通过ord()获得)

  4. 索引和切片除了不可以修改字符串中的字符,其他用法与列表相同

  5. 大小写转换

 s1 = 'hello, world!'
 
 # 使用capitalize方法获得字符串首字母大写后的字符串
 print(s1.capitalize())   # Hello, world!
 # 使用title方法获得字符串每个单词首字母大写后的字符串
 print(s1.title())        # Hello, World!
 # 使用upper方法获得字符串大写后的字符串
 print(s1.upper())        # HELLO, WORLD!
 
 s2 = 'GOODBYE'
 # 使用lower方法获得字符串小写后的字符串
 print(s2.lower())        # goodbye
  1. 查找
 s = 'hello, world!'
 
 # find方法从字符串中查找另一个字符串所在的位置
 # 找到了返回字符串中另一个字符串首字符的索引
 print(s.find('or'))        # 8
 # 找不到返回-1
 print(s.find('shit'))      # -1
 # index方法与find方法类似
 # 找到了返回字符串中另一个字符串首字符的索引
 print(s.index('or'))       # 8
 # 找不到引发异常
 print(s.index('shit'))     # ValueError: substring not found
 s = 'hello good world!'
 # 从前向后查找字符o出现的位置(相当于第一次出现)
 print(s.find('o'))       # 4
 # 从索引为5的位置开始查找字符o出现的位置
 print(s.find('o', 5))    # 7
 # 从后向前查找字符o出现的位置(相当于最后一次出现)
 print(s.rfind('o'))      # 12
  1. 性质判断
  s1 = 'hello, world!'
  # startwith方法检查字符串是否以指定的字符串开头返回布尔值
  print(s1.startswith('He'))    
  # Falseprint(s1.startswith('hel'))   # True
  # endswith方法检查字符串是否以指定的字符串结尾返回布尔值
  print(s1.endswith('!'))       # True
  s2 = 'abc123456'
  # isdigit方法检查字符串是否由数字构成返回布尔值
  print(s2.isdigit())    # False
  # isalpha方法检查字符串是否以字母构成返回布尔值
  print(s2.isalpha())    # False
  # isalnum方法检查字符串是否以数字和字母构成返回布尔值
  print(s2.isalnum())    # True
  1. 格式化字符串

    s = 'hello, world'
    
    # center方法以宽度20将字符串居中并在两侧填充*
    print(s.center(20, '*'))  # ****hello, world****
    # rjust方法以宽度20将字符串右对齐并在左侧填充空格
    print(s.rjust(20))        #         hello, world
    # ljust方法以宽度20将字符串左对齐并在右侧填充~
    print(s.ljust(20, '~'))   # hello, world~~~~~~~~
    
    变量值占位符格式化结果说明
    3.1415926{:.2f}'3.14'保留小数点后两位
    3.1415926{:+.2f}'+3.14'带符号保留小数点后两位
    -1{:+.2f}'-1.00'带符号保留小数点后两位
    3.1415926{:.0f}'3'不带小数
    123{:0>10d}0000000123左边补0,补够10位
    123{:x<10d}123xxxxxxx右边补x ,补够10位
    123{:>10d}' 123'左边补空格,补够10位
    123{:<10d}'123 '右边补空格,补够10位
    123456789{:,}'123,456,789'逗号分隔格式
    0.123{:.2%}'12.30%'百分比格式
    123456789{:.2e}'1.23e+08'科学计数法格式
  2. 通过字符串进行拆分

content = 'You go your way, I will go mine.'
content2 = content.replace(',', '').replace('.', '')
# 用空格拆分字符串得到一个列表
words = content2.split()
print(words, len(words))
for word in words:
    print(word)

# 用空格拆分字符串,最多允许拆分3次
words = content2.split(' ', maxsplit=3)
print(words, len(words))

# 从右向左进行字符串拆分,做多允许拆分3次
words = content2.rsplit(' ', maxsplit=3)
print(words, len(words))

# 用逗号拆分字符串
items = content.split(',')
for item in items:
    print(item)

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

踏墟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值