Python

Python基础(20) - 字符串支持的基本操作

2020-02-28  本文已影响0人  xianling_he

字符串常用操作 - 索引

s1 = 'hello world'
print(s1[0])
print(s1[1])
image.png

字符串常用操作 - 分片

s1 = 'hello world'
print(s1[6:9])
image.png
s1 = 'hello world'
print(s1[6:])
image.png
s1 = 'hello world'
print(s1[::2])
image.png
s1 = 'hello world'
print(s1[::1])
image.png
s1 = 'hello world'
print(s1[::-1])
image.png

字符串常用操作 - 乘法

s1 = 'hello world '
print(s1 * 10)
image.png

字符串常用操作 - 运算符in, not in的使用

s1 = 'abcdefg'
print('a' in s1)
print('x' not in s1)
image.png

字符串常用操作 - 获得字符串的长度len

s1 = 'abcdefg'
print(len(s1))
image.png

字符串常用操作 - 获得字符串的大小min,max

s1 = 'abcdefg'
print(min(s1))
print(max(s1))
image.png

总结

  1. 以上常用的字符串方法也能够适用于列表
  2. 字符串跟元组的特点是不能修改其值,但是列表可以单独赋值
  3. 列表赋值方法如S[0] = 10

加油 2020-2-28

上一篇下一篇

猜你喜欢

热点阅读