字符串
1 形式
单引号括起来的字符串: hello
双引号括起来的字符串:"Hello"
三引号括起来的字符串: hello(三单引号),"""hello"""(三双引号)
三引号括起来的字符串可以换行
2 下标(索引)
从左往右, 下标从 0 开始;
从右往右, 下标从 - 1 开始
- a = abc
- print(a[0]) ## 输出结果: a
- print(a[-1]) ## 输出结果: c
3 切片 -- 相当于字符串的截取
语法: 字符串[起始索引: 结束索引: 步长] --- 不包含结束索引的字符
列表元祖字符串都有切片操作!!
格式: info[开始索引: 结束索引]
不包括结束索引的字符
- info = life is short, you need pyhton
- print(info[0:4])
- print(info[5:]) ## 5 到结束字符
- print(info[:10]) # 开始到 10 位置
- print(info[1:10:2]) ## 2 表示步长
- print(info[::2]) ## 隔一个取一个
- print(info[::-1]) ## 倒序!!!
字符串的常用操作:
无论对字符串进行怎样的操作, 原字符串一定是不变的!! 一般是生成了新的字符串
字符串的不可变性 !!
1strip() ---- 去除空格
- a = abc def
- print(a.strip()) ## 去除全部空格
- print(a.lstrip()) ## 去除左边的空格
- print(a.rstrip()) ## 去除右边的空格
2upperlower --- 大小写转换
- b = abCDE
- print(a.lower()) ## 输出结果是: abcde
- print(a.upper()) ## 输出的结果: ABCDE
- ## 判断是否是大写或小写:
isupper()和 islower()
3endswith() startswith() --- 判断以 xx 开头或者以 xx 结尾
- b= ASD_234
- print(b.endswith(234)) ## 输出结果是: True
- print(b.startswith(AS)) ## 输出结果是: True
4join() 字符串的拼接
- print(.join([hello,world])) ## 输出的结果是: helloworld
- ## 更加常用的拼接方法:
- a = abc
- b = efgr
- print(a+b) ## 输出的结果是: abcefgr
5 各种常用判断数字字母空格
- ## 判断是否都是数字
- print(123.isdigit())
- print(123.isdecimal())
- print(123.isnumeric())
- ## 判断是否是空格
- print( .isapace()) ## True
- ## 判断是否都是字符组成
- print(abcdf.isalpha())
- ## 判断是否是有数字和字符组成
- print(fff123.isalnum())
总结: 以上的几个方法都可以配合 for 循环, 遍历之后来一个个判断所有字符的情况, 用于统计字符的个数啥的
6split() ----- 分割
分割后的结果不包含分割符, 结果返回一个列表
- name = I love you
- print(name.split( )) ## 输出的结果是:[I, love, you]
- ## 若是分割字符不存在, 就直接将字符串整体给列表
- print(name.split(x)) ## 输出的结果是:[I love you]
小例子: 假设输入: 10*20 求计算结果
- shuzi = 10-20
- if shuzi.find(*) !=-1: ## 找到了 *
- l = shuzi.split(*) ## 分割, 返回的结果是: l = [10.20]
- v = int(l[0])*int(l[1])
- print(V) ## 输出的结果是: 200
还有一个分割: splitlines()--- 按照换行符进行分割!!
7replace() ----- 替换
替换之后, 生成了一个新的字符串, 原来的字符串不变
- ## 默认替换所有指定字符
- info = life is shorts
- print(info.replace(is,was)) ## 输出: life was shorts
- ## 指定替换次数
- print(info.replace(s,S,1)) ## 输出: life is Shorts
8find() ----- 查找
如果存在, 就返回第一个字符的索引;
如果不存在, 就返回 - 1
- info = life is short, you need pyhton
- print(info.find(is))
- print(info.find(iii))
- print(info.find(is,10,20)) ## 指定范围
- ##index() 也是查找, 若是不存在, 会报异常
- print(info.index(sss))
- ## 指定范围
- print(info.index(is,4,20))
- # ## 获取指定的内容出现的次数
- # ## 不存在返回 0 次
- ## 也可以指定范围
- print(info.count(s))
- print(info.count(s,10,15))
8format() ----- 格式化字符串
在 format 函数中, 使用 {} 充当格式化操作符
- print({},{}.format(chuhao,20)) ##chuhao,20
- print({1},{0}.format(chuhao,20)) ##20,chuhao
- print({1},{0},{1}.format(chuhao,20)) ##20,chuhao,20
来源: http://www.bubuko.com/infodetail-2516626.html