字符串[不可变]

重点方法: in
count()
center(50,'*')
startswith('h')
endwith('h')
find('r') : 找不到返回 -1
index('r'): 找不到会报错
lower()
upper(')
strip()
replace('itle','lesson',1)
split('i',1)

实例:

#  * 重复输出字符串
print("hello\t"*2) # hello hello
# 通过索引获取字符串中字符,这里和列表的切片操作是相同的
print("hello"[2:4]) # ll
# in 成员运算符 - 如果字符串中包含给定的字符返回 True
print('el' in 'hello') # True
print('hello' in ['hello', 'world', '2017']) # True
# % 格式字符串, 字符注意添加引号
print('%s is a China Company' % 'Huawei') # Huawei is a China Company
# + 字符串拼接
print('hello\t'+"world2017") # hello world2017
# join --> + 效率低,该用join,join里面的必须是迭代器,所以必须是列表,元组等’
# 字符串拼接用join 【列表变字符串】
a = 'hello'
b = 'world2018'
# 用******连接a,b
print('******'.join([a, b])) # hello******world2018
print(''.join(('hello\t', 'world2019'))) # hello world2019
print(''.join(['hello\t', 'world2019'])) # hello world2019
print('***'.join(('hello'.strip()+'I love'))) # h***e***l***l***o***I*** ***l***o***v***e
print('******'.join(('hello', 'I love'))) # hello******I love # 字符串的切割,返回一个列表 【字符串变列表】
print('hello world 2017'.split()) # ['hello', 'world', '2017']
print('hello world 2017'.rsplit('l',2)) # ['hel', 'o wor', 'd 2017']
# 字符串的右切割,返回一个列表 【字符串变列表】
print('hello world 2017'.rsplit('l')) # ['he', '', 'o wor', 'd 2017']
# 首字母大写
print('hello'.capitalize()) # Hello
# string.count(str, beg=0, end=len(string))
print('helloworldhelloworld'.count('l', 2, 22)) # 6
# string.decode(encoding='UTF-8', errors='strict') 以 encoding 指定的编码格式解码 string,如果出错默认报一个 ValueError 的 异 常 , 除 非 errors 指 定 的 是 'ignore' 或 者'replace'
# string.encode(encoding='UTF-8', errors='strict') 以 encoding 指定的编码格式编码 string,如果出错默认报一个ValueError 的异常,除非 errors 指定的是'ignore'或者'replace' # string.endswith(obj, beg=0, end=len(string))
print('hello'.endswith('o')) # True
# string.startswith(obj, beg=0, end=len(string))
print('hello'.startswith('h')) # True
# string.expandtabs(tabsize=8), 把字符串 string 中的 tab 符号转为空格,默认是8
print('h ello'.expandtabs(tabsize=8)) # h ello
# string.find(str, beg=0, end=len(string)),找不到不报错,返回-1
print('hello'.find('llo')) # 返回2
print('hello'.find('xyz')) # 返回-1,表示查找不到
# 从右开始匹配,返回的字符串原来的位置,真实的索引位置
print('hello'.rfind('l')) # 3
# 查找字符所在的位置, 找不到会报错
print('hello'.index('o')) # 4
# 格式化输出 format
print('hello {name} {year}'.format(name='world', year='2019')) # hello world 2019
# 格式化输出 format_map,要求用字典表示
print('hello {name} {year}'.format_map({'name': 'world', 'year': '{2020}'})) # hello world {2020}
# 判读字符串是否包含数字和字母,至少一个字符 ==> Java里面的正则 \w
print('hello1235'.isalnum()) # True
# 判断是否是个十进制的数字
print('hello2451'.isdecimal()) # False
print('123'.isdecimal()) # True
# 判断是否是一个字母,且至少有一个字符
print("hello".isalpha()) # True
# 判断是否是个数字
print('123'.isdigit()) # True
# 判断是否是个数字
print('9999'.isnumeric()) # True
# 判断非法字符
print('23-hl'.isidentifier()) # True
# 判断字符串是否全是小写
print('Abc'.islower()) # False
# 判断字符串是否全是大写
print('Abc'.isupper()) # False
# 判断是否全是一个空格,只包含空格
print(' hello'.isspace()) # False
# 判断是否首字母大写
print("Hello World".istitle()) # True
print("Hello WOrld".istitle()) # False
# 所有的大写变小写
print("HELLoWorld".lower()) # helloworld
# 所有的小写变大写
print("helloworld".upper()) # HELLOWORLD
# 大写变小写,小写变大写
print('Hello WORLD'.swapcase()) # hELLO world
# 剧中对齐,空格用*填充
print('hello'.center(50, '*'))
# 全部靠左对齐
print("Hello world".ljust(50, '*'))
# 全部靠右对齐
print("Hello world".rjust(50, '*'))
# 去除左右的空格和换行符
print(" Hello World\n ".strip()) # Hello World
# 去除左的空格和换行符
print(" Hello World\n ".lstrip()) # Hello World
# 去除右的空格和换行符
print(" Hello World\n ".rstrip()) # Hello World
# 字符串的替换
print("hello world".replace('llo', 'TTT')) # heTTT world
print("hello world".replace('l', 'Z', 2)) # heZZo world 只替换2次,第三个l忽略
# 按照title的格式输出
print('hello world'.title()) # Hello World

最新文章

  1. Markdown使用指南(1)——基础语法
  2. Oracle查询DQL脚本记录
  3. RFS_关键字
  4. HDUOJ--汉诺塔II
  5. (转)Android系统自带Activity样式(@android:style/)
  6. Java基础知识强化71:正则表达式之基本规则 和 常用正则表达式
  7. java基础:数组的复制
  8. JSONObject简介(2)
  9. 201521123019《Java程序设计》第1周学习总结
  10. mysql-SQL优化总结
  11. php+xdebug远程调试(单人)
  12. hadoop day 6
  13. Android 自定义弹出框带EditText
  14. Leetcode 344.反转字符串 By Python
  15. 针对于多个inner join或者left join多条件查询的时候,各个inner join 的指向问题
  16. RecyclerView使用技巧(item动画及嵌套高度适配解决方案)
  17. Java编程的逻辑 (18) - 为什么说继承是把双刃剑
  18. checkbox复选框的一些深入研究与理解
  19. java.io.IOException: Could not find status of job:job_1534233312603_0002
  20. 在Flex (Flash)中嵌入HTML 代码或页面—Flex IFrame

热门文章

  1. Oracle PL/SQL编程之函数
  2. 013-PaymentUtils工具类模板
  3. SQL Cookbook—字符串
  4. Bash编程(4) 参数与变量
  5. Bash编程(2) 循环与分支
  6. Iterator和for...of循环
  7. [转]Using OData from ASP.NET
  8. [转]微信小程序联盟 跳坑《一百八十一》设置API:wx.openSetting使用说明
  9. Nginx配置整理
  10. 在 Azure 上创建和链接 MySQL 数据库