字符串:有序的字符的集合,用于存储和表示基本的文本信息,一对单、双、或三引号中间包含的内容称之为字符串

1.特性:有序,不可变(开辟新地址存储字符串,python解释器会定期清空不用了的已存储的)

>>> a = 'alex'
>>> id(a)
1806635111064
>>> a = 'Jack'
>>> a
'Jack'
>>> id(a)
1806635111232

 2.操作  常用:isdigit  replace  find  count  strip  center  split  format  join 

不常用:(数据)swapcase capitalize casefold center expandtabs ljust rjust zfill marketrans translate partition replace rstrip lstrip rsplit splitliness

              (判断)endswith isalnum isdecimal isnumeric isidentifier islower isupper isprintable isspace istitle

(1)得出数据的操作:

 >>> s = 'Hello World!'
>>> s.swapcase() #大小写反过来
'hELLO wORLD!'
>>> s = 'Hello World!'
>>> s.capitalize() #第一个字母大写,其余全部小写
'Hello world!'
>>> s.casefold() #变成统一的小写字母
'hello world!'
>>> s.center(50,'-') #把s放到中间,两边用'-'填充,总长度为50
'-------------------Hello World!-------------------'
>>> s.count('o') #统计字符'o'的数量
2
>>> s.count('o',0,5) #统计从0到5字符'o'的数量,顾头不顾尾
1
>>> s2 = 'a\tb' #\b是Tab键
>>> s2
'a\tb'
>>> print(s2)
a b
>>> s2.expandtabs()
'a b'
>>> s2.expandtabs(20) #将Tab键的长度扩展到20
'a b'
>>> s.find('o') #查找字符o 找到返回索引值,找不到返回负数
4
>>> s.find('op')
-1
>>> s.find('o',0,3) #从0到3查找字符o,找到返回从左边找到的第一个索引值,找不到返回-1,rfind -> 从右边开始数
-1
>>> s.find('o',0,5) #顾头不顾尾
4
>>> s3 = 'my name is {0}, i am {1} years old.'
>>> s3
'my name is {0}, i am {1} years old.'
>>> s3.format('Alex','') #格式化,用括号里的依次代替{0}{1},有点像列表
'my name is Alex, i am 22 years old.'
>>> s3 = 'my name is {0}, his name is {0}.'
>>> s3.format('Alex') #括号里的第一个就是{0},两个{0}一样
'my name is Alex, his name is Alex.'
>>> s3.format('Alex','Jane')
'my name is Alex, his name is Alex.'
>>> s3 = 'my name is {name}, i am {age} years old.'
>>> s3.format(name = 'Alex',age = '') #根据定义命名来赋值,直接写是不行的
'my name is Alex, i am 22 years old.'
>>> s.index('o') #返回第一个o的索引值,rindex -> 从右边开始数,其余一致
4
>>> s.index('o',5,6) #从5到6返回从左边数第一个o的索引值,没有就报错
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> names = ['alex','jack','rain']
>>> '-'.join(names) #以 - 来将names里的元素拼接起来,并且进行区分
'alex-jack-rain'
>>> ''.join(names)
'alexjackrain'
>>> ','.join(names)
'alex,jack,rain'
>>> s = 'Hello World'
>>> s.ljust(50) #把字符串的长度变成50,长度不够,就在字符串窜后面用空格来填
'Hello World '
>>> s.ljust(50,'-') #把字符串的长度变成50,长度不够,就在字符串后面用 - 来填,rjust与ljust相反,rjust在左边填 -
'Hello World---------------------------------------'
>>> s.zfill(40) #把s长度变成40,不够的在前面用0y填,一般是底层二进制用到
'00000000000000000000000000000hello world'
>>> s = '\n hello world '
>>> s
'\n hello world '
>>> s.strip() #去掉前后的空格 lstrip ->只去掉左边的空格 rstrip ->只去掉右边的空格
'hello world'
>>> str_in = 'abcdef'
>>> str_out = '!@#$%^' # str_in和str_out的字符数量必须一致,如此才能一一对应
>>> str.maketrans(str_in,str_out) #maketrans生成密码对应表,表中为ASCII码
{97: 33, 98: 64, 99: 35, 100: 36, 101: 37, 102: 94}
>>> table = str.maketrans(str_in,str_out)
>>> table
{97: 33, 98: 64, 99: 35, 100: 36, 101: 37, 102: 94}
>>> s = 'hello world'
>>> s.translate(table) #将s按照table进行翻译
'h%llo worl$'
>>> s.partition('o') #将字符串以从左找到的第一个'o'分成两半,rpartition以从右边找到的第一个'o'分成两半
('hell', 'o', ' world')
>>> s.replace('o','-') #把'o'全部换成'-'
'hell- w-rld'
>>> s.replace('o','-',1) #把'o'换成'-',只换一次
'hell- world'
>>> s.split() #以空格将s分开,放入列表
['hello', 'world']
>>> s.split('o') #以'o'将s分开,放入列表,'o'消失
['hell', ' w', 'rld']
>>> s.split('o',1) #以左边第一个'o'将s分开,放入列表,'o'消失,rsplit -> 从右边开始数 , splitlines -> 以行,即'\n'来分
['hell', ' world']

(2)判断 True or False 的操作

 >>> s.endswith('!') #s是否以!结尾,startswith -> 以...开始
True
>>> s.endswith('!sdf') #s是否以!sdf结尾
False
>>> ''.isalnum() #判断是否是数字和字符,不包括特殊符号
True
>>> '22s'.isalnum()
True
>>> '22s!'.isalnum()
False
>>> ''.isdecimal() #判断是否是整数,用法和isdigit、isnumeric
True
>>> '33s'.isdecimal()
False
>>> '33.3'.isdecimal()
False
>>> ''.isidentifier() #是否是合法的变量名,合法变量名条件:以下划线、字母、数字组成,开头不能是数字,不能纯用数字组合
False
>>> '3asd'.islower() #字母是否都是小写字母
True
>>> '2asd56ER'.islower()
False
>>> ''.isprintable() #能否被打印,只有文本文件,字节格式的能被打印,纯二进制文件之类的不能被打印(Linux中涉及)
True
>>> ' '.isspace() #是否是空格
True
>>> '3de '.isspace()
False
>>> 'Hello World'.istitle() #单词的第一个字符是否都是大写,就像新闻标题一样
True
>>> '45ASD'.isupper() #字母是否都是大写
True
>>> '45sA'.isupper()
False

最新文章

  1. 移动web开发调试工具AlloyLever介绍
  2. CSS3硬件加速需要注意的事项
  3. Ruby中 使用Builder Xml Markup 操作XML
  4. 20145334赵文豪 《Java程序设计》第3周学习总结
  5. 解决eclipse+git中每次clean项目需要重新commit文件
  6. Microsoft Dynamics AX 2009 White Paper: Close Non-Financial Transfers
  7. Top Things to Consider When Troubleshooting Complex Application Issues
  8. 1.linux概述及如何访问
  9. 黑马程序员-C#学习笔记(二)
  10. Actions 动作集
  11. cf448B Suffix Structures
  12. Windows Phone 8本地化多语言支持
  13. linux和windows双系统时间错误解决方法
  14. 新博客在SEO方面需要注意哪几点?
  15. C#+EntityFramework编程方式详细之Code First
  16. [每天解决一问题系列 - 0010] ADB Not Responding - Android Studio
  17. [knowledge][ETA] Encrypted Traffic Analytics
  18. RabbitMQ进阶使用-延时队列的配置(Spring Boot)
  19. 网络对抗技术 2017-2018-2 20155215 Exp9 Web安全基础
  20. mysql三大特性、三范式、五大约束

热门文章

  1. [译]课程 1: 使用 Quartz
  2. visualSFM
  3. ActiveReports 大数据分析报告:贸易争端与中国企业数字化转型
  4. python非官方模块下载大全
  5. 『TensorFlow』张量拼接_调整维度_切片
  6. 数据分析库之Numpy
  7. 若依项目利用nginx实现负载均衡及保持会话
  8. 移动端滑动轮播,原生JS
  9. 解决pycharm添加第三方包失败
  10. Awesome Tools