0
点赞
收藏
分享

微信扫一扫

day9字符串

科牛 2022-04-24 阅读 106
python

day9字符串

01字符串相关函数

1. len

2.str

  • str(数据) - 所有的数据都可以转换成字符串;转换的时候是在数据的打印值外面加引号

3.eval(字符串) - 获取指定字符串引号中的内容(去掉字符串的引号)

注意:这儿的字符串去掉引号以后必须是一个合法的表达式

1)去掉引号以后是一个合法的数据
result = eval('100')  # 100
print(result, result * 2, result + 10)

# result = eval('abc') # 报错 abc不加引号不符合python语言规范
result = eval('"abc"')  # "abc" 从5个元素的字符串“abc”变成3个长度的abc
print(result, len(result))

result = eval('[10, 20, 30]')  # [10, 20, 30]列表
result.append(100)
print(result)  # [10, 20, 30, 100]支持列表操作
2) 去掉引号以后是合法的表达式
result = eval('100 + 200')  # 100 + 200
print(result) # 300

02字符串相关操作

1.join

  • 字符串.join(序列) - 将序列中的元素用指定的字符串连接成一个新的字符(序列中的元素必须都是字符串)
list1 = ['hello', 'world', '你好', '世界']

result = ''.join(list1)
print(result)  # helloworld你好世界

result = ','.join(list1)
print(result)  # hello,world,你好,世界

result = ' '.join(list1)
print(result)  # hello world 你好 世界
# 练习:将num中的元素拼接成一个数字
nums = [10, 20, 30, 4, 52]
result = ''.join([str(x) for x in nums])
print(int(result))  # 102030452
# 练习:把个位数拼接起来
nums = [10, 29, 30, 4, 52]
result = ''.join([str(x % 10) for x in nums])
print(result)  # 09042
# 练习:把数字取出来相加并输出结果
list1 = [19, 'abc', True, 1.23, 8, 22.2, '环境']
result = '+'.join([str(x) for x in list1 if type(x) == int or type(x) == float])
print(result, eval(result), sep='=')  # 19+1.23+8+22.2=50.43
# 注意:如果遍历出现多种同种类型可以这样写 result = '+'.join([str(x) for x in list1 if type(x) in (int, float)])

2.split - 切割

  • 字符串1.split(字符串2) - 将字符串1中所有的字符串2作为切割点对字符串1进行切割
str1 = 'abc123hello123你好123Python'
result = str1.split('123')
print(result)  # ['abc', 'hello', '你好', 'Python']
  • 注意:如果切割点在字符串开头、或者字符串结尾、或者连续出现多个切割点都会产生空串
str1 = '123abc123hello123123你好123Python123'
print(str1.split(('123')))  # ['', 'abc', 'hello', '', '你好', 'Python', '']

str1 = '123abc123hello123123你好123Python123'
print(str1.split('123', 2))  # ['', 'abc', 'hello123123你好123Python123']

3.replace - 替换

  • 字符串1.replace(字符串2, 字符串3) - 将字符串1中所有的字符串2都替换成字符串3
str1 = 'how are you? I am fine, Thank you! you see see, one day day'
# you -> me
result = str1.replace('you', 'me')
print(result)  # how are me? I am fine, Thank me! me see see, one day day

result = str1.replace('you', 'me', 1)
print(result)  # how are me? I am fine, Thank you! you see see, one day day

4.strip - 去掉字符串前后的空白字符

  • 字符串.strip()
  • 字符串.strip(字符)
str1 = '           \t  \n       good good study         \n'
result = str1.strip()
print(result)  # good good study

str2 = '*********good good study, day day up!*********'
result = str2.strip('*')
print(result)  # good good study, day day up!

5.count

  • 字符串1.count(字符串2) - 统计字符串1中字符串2出现的次数
str1 = 'how are you? I am fine, Thank you! you see see, one day day'
print(str1.count('you'))  # 3
print(str1.count('a'))  # 5

6.maketrans、translate

  • str.maketrans(字符串1,字符串2) - 通过字符串1和字符串2创建替换对应关系表

  • 字符串.translate(对应关系表) - 按照对应关系表将字符串中字符进行替换

table = str.maketrans('abc', '123')  # a-1,b-2,c-3
str1 = 'aamnbcabcmn'
result = str1.translate(table)  # 11mn23123mn
print(result)

7.

1)center、ljust、rjust、zfill

  • 字符串.center(长度, 字符) - 将指定字符串变成指定长度,不够的用指定字符填充;原字符串放中间
  • 字符串.ljust(长度, 字符) - 将指定字符串变成指定长度,不够的用指定字符填充;原字符串放左边
  • 字符串.rjust(长度, 字符) - 将指定字符串变成指定长度,不够的用指定字符填充;原字符串放右边
  • 字符串.zfill(长度) == 字符串.rjust(长度,‘0’) -比如生活中用于编号
str1 = 'abc'
print(str1.center(7, 'x'))  # xxabcxx
print(str1.ljust(7, 'x'))  # abcxxxx
print(str1.rjust(7, 'x'))  # xxxxabc

num = '123'
new_num = num.zfill(5)
print(new_num)  # 00123

2)find、index

  • 字符串1.find(字符串2) - 获取字符串2在字符串1中第一次出现的位置
    字符串1.index(字符串2)- 获取字符串2在字符串1中第一次出现的位置

  • 字符串1.rfind(字符串2)
    字符串1.index(字符串2)

str1 = 'how are you? I am fine, Thank you! you see see, one day day'
print(str1.find('you'))  # 8
print(str1.index('you'))  # 8

print(str1.find('youu'))  # -1
# print(str1.index('youu'))#报错!

3)isdigit() isnumeric()-判断是否是纯数字字符串

  • 字符串.isdigit() - 判断字符串中的元素是否全是数字字符(0-9)

  • 字符串.isnumeric()- 判断字符串中的元素是否全是具有数字意义的字符

str2 = '2342'
print(str1.isdigit()) #False
print(str1.isnumeric())#False

str2 = '一'
print(str2.isdigit())#False
print(str2.isnumeric())#True

03格式字符串

name = input('请输入姓名:')
age = int(input('请输入年龄:'))
# 问题:写代码的时候可能会出现一个字符串中的部分内容无法确定
# xxx今年xx岁!
  • 方法1:字符串拼接
str1 = name + '今年' + str(age) + '岁!'
print(str1)
  • 方法2:格式占位符
str1 = '%s今年%d岁!' % (name, age)
print(str1)
  • 方法3:使用f-string
str1 = f'{name}今年{age}岁!'
print(str1)
1.格式占位符创建字符串
  • 语法:包含格式占位符的字符串 % (数据1, 数据2, 数据3,…)

  • 注意:后面括号中的数据必须和前面字符串中的占位符一一对应

  • 常用的格式占位符:

    • %s - 字符串占位符,可以对应任何类型的数据
    • %d - 整数占位符,可以对应任何数字
    • %f - 浮点数占位符,可以对应任何数字
name = '冉'
age = 18
str1 = '%s今年%d岁, 月薪:%.2f元!' % (name, age, 10890)
print(str1)
price = 34.889

str2 = '价格:%s' % price
print(str2)  # 价格:34.889

str2 = '价格:%d' % price
print(str2)  # 价格:34

str2 = '价格:%f' % price
print(str2)  # 价格:34.889000

str2 = '价格:%.2f' % price
print(str2)  # 价格:34.89
2.f-string
  • 语法:在字符串最前面加f,就可以在字符串中通过’{数据}'来提供字符串中变化的部分
name = '小明'

str1 = '{小明}'
print(str1)  # {小明}

str1 = f'{name * 2}'
print(str1)  # 小明小明

str1 = f'{name[1] * 3}'
print(str1)  # 明明明

age = 18
str1 = f'{name}今年{age}岁!'
print(str1)  # 小明今年18岁!

score = [90, 89, 67]
str1 = f'{name}三门学科的分数分别是{str(score)[1:-1]}'
print(str1)  # 小明三门学科的分数分别是90, 89, 67
  • 1)加参数:{表达式:参数}

    • a.控制小数位数的参数:{表达式:.Nf参数}
# a.控制小数位数的参数:{表达式:.Nf参数}
money = 19030
str1 = f'年薪:{money * 13:.2f}元'
print(str1)  # 年薪:247390.00元
    • b.显示百分比: {表达式:.N%}
# b.显示百分比: {表达式:.N%}
rate = 0.87
str1 = f'班级及格率:{rate:.2%}'
print(str1)  # 班级及格率:87.00%
    • c.逗号显示金额:{表达式:,}
money = 1903000
str1 = f'年薪:{money * 13:,.2f}元'
print(str1) # 年薪:24,739,000.00元
    • d.修改填充内容的长度:{表达式:字符> 长度} - 字符放在前
    • {表达式:字符> 长度} - 字符放在后
    • {表达式:字符> 长度} - 字符放在中
num = 23
str1 = f'py2202{num:0>4}'
print(str1) # py22020023

str1 = f'py2202{num:0<4}'
print(str1) # py22022300

str1 = f'py2202{num:0^6}'
print(str1) # py2202002300

str1 = f'py2202{num:#>4}'
print(str1) # py2202##23
举报

相关推荐

day9集合和字符串

2.13字符串对齐

day9-字符串作业

day9-字符串总结

day9-字符串练习

day9-字符串作业(1)

0 条评论