博客
关于我
Python基础之str常用方法、for循环
阅读量:450 次
发布时间:2019-03-06

本文共 1642 字,大约阅读时间需要 5 分钟。

初学Python时,字符串操作可能会让人感到困惑,但理解这些基本概念后,处理文本数据会变得更加高效。以下是一些关键点和示例,帮助你更好地掌握Python字符串操作。

1. 字符串索引与切片

索引案例

s = 'abcd's1 = s[0]  # 输出 'a'

切片案例

s = 'abcd's2 = s[0:3]  # 输出 'abc',不包括索引3的字符s3 = s[-1]   # 输出 'd'

取整个字符串

s = 'abcd's4 = s[0:]    # 输出 'abcd's5 = s[:]     # 输出 'abcd's6 = s[0:0]   # 输出空字符串

步长

s = 'abcde's6 = s[0:4:2]  # 输出 'ac'

逆序

s = 'abcde's7 = s[-1::-1]  # 输出 'edcba'

2. 大写、小写转换

s = 'AbCd's.swapcase()  # 输出 'aBcD'

3. 字符串居中、空白填充

s = 'AAA's1 = s.center(20)  # 居中,总长度为20s2 = s.center(20, '*')  # 居中,填充字符为*

4. 补位

s = 'ab\tcd's1 = s.expandtabs()  # 输出 'ab      cd'

5. 公共方法

s = 'abcd我'print(len(s))  # 输出5

6. 判断开头和结尾

s = 'abcd's1 = s.startswith('a')  # Trues2 = s.startswith('ab')  # Trues3 = s.startswith('abc')  # Trues4 = s.endswith()  # 判断是否以空字符串结尾,默认返回False

7. 查找字符位置

s = 'abcdacd's1 = s.find('c')  # 输出2

8. 删除空格

s = '  abcd  's.strip()  # 输出 'abcd's.strip('*')  # 输出 'abcd's.strip(' %*')  # 输出 'abcd'

9. 统计字符个数

s = 'abbcd's.count('a')  # 输出1s.count('bb')  # 输出1

10. 分割成列表

s = 'a b c d's.split()  # 输出 ['a', 'b', 'c', 'd']s.split(';')  # 输出 ['', 'a', 'b', 'c', 'd']

11. 格式化输出

s = '我叫{},今年{}'.format('sun', '19')  # 输出 '我叫sun,今年19's = '我叫{0},今年{1}'.format('sun', '19')  # 输出 '我叫sun,今年19's = '我叫{name},今年{age}'.format(age=19, name='sun')  # 输出 '我叫sun,今年19'

12. 替换

s = 'abacda's.replace('a', 'A')  # 输出 'ABacda's.replace('a', 'A', 1)  # 输出 'ABacda'

13. 判断字符串内容

s = '123's.isnum()  # Trues.isalpha()  # Falses.isalnum()  # True

14. 循环

s = 'abcd'for i in s:    print(i)

15. while循环

s = 'abcdacd'i = 0while i < len(s):    print(s[i])    i += 1

16. 判断字符串包含

s = 'azxczcx政治'if '政治' in s:    print('含有敏感词')

通过这些示例,你可以开始在Python中灵活使用字符串操作,处理各种文本数据问题。

转载地址:http://opofz.baihongyu.com/

你可能感兴趣的文章
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>