python的字符串基本操作(笔记专用仅供参考)

目录

1、切片

2、原始字符串

3、字符串重复

4、in

5、去空格

6、分隔字符串

7、拼接字符串

8、统计字符串里某个字符出现的次数

9、检测字符串中是否包含子字符串

10、判断字符串是否以指定前缀、后缀结尾

11、根据指定的分隔符将字符串进行分割

12、替换字符串

13、检测字符串组成

14、字符串处理

15、居中填充

16、靠右填充

17、输出格式


1、切片

1

2

3

4

5

6

7

# str[beg:end]

# (下标从 0 开始)从下标为beg开始算起,切取到下标为 end-1 的元素,切取的区间为 [beg, end)

str = ' python str '

print (str[3:6])  # tho

# str[beg:end:step]

# 取 [beg, end) 之间的元素,每隔 step 个取一个

print (str[2:7:2]) # yhn

2、原始字符串

1

2

3

# 在字符串前加 r/R

# 所有的字符串都是直接按照字面的意思来使用,没有转义特殊或不能打印的字符

print (r'\n')  # \n

3、字符串重复

1

2

3

4

5

# str * n, n * str

# n 为一个 int 数字

str = "hi"

print (str*2)  # hihi

print (2*str)  # hihi

4、in

1

2

3

4

str = ' python'

print ('p' in str)  # True

print ('py' in str)  # True

print ('py' not in str) # False

5、去空格

1

2

3

4

5

6

7

8

str = ' python str '

print (str)

# 去首尾空格

print (str.strip())

# 去左侧空格

print (str.lstrip())

# 去右侧空格

print (str.rstrip())

6、分隔字符串

1

2

3

4

5

6

7

8

9

10

11

12

13

14

str = ' 1 , 2 , 3 , 4 , 5 , '

# 默认使用空格分隔

print (str.split())  # ['1', ',', '2', ',', '3', ',', '4', ',', '5', ',']

# 指定使用空格进行分隔,首尾如果有空格,则会出现在结果中

print (str.split(' ')) # ['', '1', ',', '2', ',', '3', ',', '4', ',', '5', ',', '']

# 指定其他字符串进行分隔

print (str.split(',')) # [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' ']

print (str.split('3 ,')) # [' 1 , 2 , ', ' 4 , 5 , ']

str = 'mississippi'

print (str.rstrip('ip'))

# 取行, python 中把 "\r","\n","\r\n",作为行分隔符

str = 'ab c\n\nde fg\rkl\r\n'

print (str.splitlines())   # ['ab c', '', 'de fg', 'kl']

print (str.splitlines(True)) # ['ab c\n', '\n', 'de fg\r', 'kl\r\n']

7、拼接字符串

1

2

3

4

# str.join()方法用于将序列中的元素以指定的字符连接生成一个新的字符串。

str = '-'

seq = ("a", "b", "c"); # 字符串序列

print (str.join(seq)) # 'a-b-c'

8、统计字符串里某个字符出现的次数

1

2

3

str = "thing example....wow!!!"

print (str.count('i', 0, 5)) # 1

print (str.count('e') ) # 2

9、检测字符串中是否包含子字符串

1

2

3

4

5

6

7

8

9

10

11

12

# str.find(str, beg=0, end=len(string))

# 如果包含子字符串返回开始的索引值,否则返回-1。

str1 = "this is string example....wow!!!"

str2 = "exam"

print (str1.find(str2))   # 15

print (str1.find(str2, 10)) # 15

print (str1.find(str2, 40)) # -1

# str.index(str, beg=0, end=len(string))

# 如果包含子字符串返回开始的索引值,否则抛出异常。

print (str1.index(str2))   # 15

print (str1.index(str2, 10)) # 15

# str.rfind(str, beg=0, end=len(string))

10、判断字符串是否以指定前缀、后缀结尾

1

2

3

4

5

6

7

8

9

10

11

12

13

14

# str.startswith(str, beg=0,end=len(string))

# 检查字符串以指定子字符串开头,如果是则返回 True,否则返回 False

str = "this is string example....wow!!!"

print (str.startswith( 'this' ))    # True

print (str.startswith( 'is', 2, 4 ))  # True

print (str.startswith( 'this', 2, 4 )) # False

# str.endswith(suffix[, start[, end]])

# 以指定后缀结尾返回True,否则返回False

suffix = "wow!!!"

print (str.endswith(suffix))    # True

print (str.endswith(suffix,20))   # True

suffix = "is"

print (str.endswith(suffix, 2, 4))  # True

print (str.endswith(suffix, 2, 6)) # False

11、根据指定的分隔符将字符串进行分割

1

2

3

4

5

6

# str.partition(del)

# 返回3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。

str = "百度一下,你就知道"

print (str.partition("://"))  # ('http', '://', 'www.baidu.com/')

# string.rpartition(str)  从右边开始

12、替换字符串

1

2

3

4

5

6

7

8

9

10

# str.replace(old, new[, max])

# 字符串中 old(旧字符串) 替换 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。

str = "thing example....wow!!! thisslly string";

print (str.replace("is", "was")) # thwas was string example....wow!!! thwas was really string

print (str.replace("is", "was", 3)) # thwas was string example....wow!!! thwas is really string

# str.expandtabs(tabsize=8)

# 把字符串中的 tab 符号('\t')转为空格,tab 符号('\t')默认的空格数是 8

13、检测字符串组成

1

2

3

4

5

6

7

8

9

10

11

# 检测数字

str.isdigit()  # 检测字符串是否只由数字组成

str.isnumeric() # 检测字符串是否只由数字组成,这种方法是只针对unicode对象

str.isalpha()  # 检测字符串是否只由字母组成

# 检测字母和数字

str.isalnum()  # 检测字符串是否由字母和数字组成

# 检测其他

str.isspace()  # 检测字符串是否只由空格组成

str.islower()  # 检测字符串是否由小写字母组成

str.isupper()  # 检测字符串中所有的字母是否都为大写

str.istitle()  # 检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写

14、字符串处理

1

2

3

4

5

6

7

8

str.capitalize()  # 将字符串的第一个字母变成大写,其他字母变小写

str.lower()    # 转换字符串中所有大写字符为小写

str.upper()    # 将字符串中的小写字母转为大写字母

str.swapcase()   # 对字符串的大小写字母进行转换

max(str)  # 返回字符串 str 中最大的字母

min(str)  # 返回字符串 str 中最小的字母

len(str)  # 返回字符串的长度

str(arg) # 将 arg 转换为 string

15、居中填充

1

2

3

4

5

# str.center(width[, fillchar])

# 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串。默认填充字符为空格

str = "this is string example....wow!!!"

print (str.center(40, 'a'))  # aaaathis is string example....wow!!!aaaa

16、靠右填充

1

2

3

4

# str.zfill(width)

# 返回指定长度的字符串,原字符串右对齐,前面填充0

str = "this is string example....wow!!!"

print (str.zfill(40))  # 00000000this is string example....wow!!!

17、输出格式

1

2

3

4

5

6

7

print ("My name is %s and weight is %d kg!" % ('Cool', 21))

# My name is Cool and weight is 21 kg!

print ('%(language)s has %(number)03d quote types.' % {"language": "Python", "number": 2})

# Python has 002 quote types.

# str.format(*args, **kwargs)

print ('{0}, {1}, {2}'.format('a', 'b', 'c')) # a, b, c

print ('{1}, {0}, {2}'.format('a', 'b', 'c')) # b, a, c

觉得有用的点个赞就行!(转载于python老师的文档资料)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

致奋斗的自己

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

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

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

打赏作者

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

抵扣说明:

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

余额充值