Python字符串运算符


Python字符串运算符:

  + :连接左右两端的字符串。

  *  :重复输出字符串。

  [ ] :通过索引获取字符串中的值。

  [start:stop:step]:开始,结束位置的后一个位置,步长。

  in  :判断左端的字符是否在右面的序列中。

  not in:判断左端的字符是否不在右面的序列中。

  r/R :在字符串开头使用,使转义字符失效。

# 字符串使用 + 号 strs = "hello " + "world." print(strs) # hello world. # 字符串使用 * 号 strs = 'abc ' # 无论数字在哪一端都可以 print(3*strs) # abc abc abc print(strs * 3) # abc abc abc # 使用索引下标 strs = "hello world." print(strs[4]) # o print(strs[7]) # o # 切片操作,左闭右开原则 strs = "hello world." # 将字符串倒序输出 print(strs[::-1]) # .dlrow olleh print(strs[6:11:]) # world  strs = "ABCDEFG" print("D" in strs) # True print("L" in strs) # False print("D" not in strs) # False print("L" not in strs) # True # 使用 r 使字符串中的转义字符失效 print('a\tb') # a b print(r'a\tb') # a\tb

2020-02-08


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM