首页 > 技术文章 > py基础-字符串,列表

mynetuser 2018-07-15 10:36 原文

1、字符串

查看字符串对象的内置属性:

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

字符串可以相加和相乘:

s1 = "123"
s2 = "abc"
print(s1+s2)
print('坚强'*8)

根据索引取字符串
s = 'alexwusirritian'
s1 = s[0]
print(s1,type(s1))
s3 = s[-1]
print(s3)
s4 = s[-2]
print(s4)

根据切片取:

s5 = s[:4]
print(s5)
s6 = s[4:9]
 print(s6)

切片:s[起始索引:结束索引+1:步长]

s8 = s[:5:2]
print(s8)
s9 = s[-1:-5:-1]
print(s9)

 

推荐阅读