Conversion between data types Int<-->str
int转str:str(digit)str转int:int(string) 注意此处的string只能是包含数字的字符串或开头有负号
Int<-->bool
bool(非零)-->Truebool(零) -->Falseint(True) -->1int(False)-->0
Bool<-->str
bool -->str没有实际意义str -->bool:bool(非空字符串) --> Truebool(空字符串) --> False注意:空字符串为'',而不是' '。
The learning of the Python string (string) The first part: The index tile step is indexed by the index, and the resulting string is a character.
>>> str1 = 'python'>>> str1[2]'t'>>> str1[-1]'n'
By slicing the value, Gu Tou regardless of the tail
>>> str2 = 'python'>>> str2[0 : -1]'pytho'>>> str2[0:]'python'
by Slice + step
>>> str3 = 'python is a language'>>> str3[::2]'pto salnug'>>> str3[::-1]'egaugnal a si nohtyp'当步长为负时,是倒着取值。
Alternatively, you can use an array to reverse the string
strA = input("请输入需要翻转的字符串:")order = [] for i in strA: order.append(i)order.reverse() #将列表反转print ''.join(order) #将list转换成字符串
The second part: the common method of string. Capitalize () First letter capital ★★☆☆☆
>>> str4 = 'hellokitty'>>> str4.capitalize()'Hellokitty'
Center () strings are populated before and after the custom character ★★☆☆☆
>>> str4 = 'hellokitty'>>> str4.center(20, '*')'*****hellokitty*****'
Upper () English full capitalization; lower () English all lowercase. It will be used on verification code verification. ★★★★★
>>> str4 = 'hello kitty'>>> str4.upper()'HELLO KITTY'>>> str5 = 'Hello Kitty'>>> str5.lower()'hello kitty'
Swapcase () Case Flip ★★☆☆☆
>>> str5 = 'Hello Kitty'>>> str5.swapcase()'hELLO kITTY'
Title () The first letter of each part that is not separated by the letter is capitalized ★★☆☆☆
>>> str6 = 'hello kitty,what can I do4you?'>>> str6.title()'Hello Kitty,What Can I Do4You?'
StartsWith () with what EndsWith () with what end ★★★★★
>>> str6 = 'hello kitty,what can I do4you?'>>> str6.startswith('h')True>>> str6.startswith('hello')True
Find finds the index by element, returns the first one, and returns without this element -1★★★★★
>>> str6 = 'hello kitty,what can I do4you?'>>> str6.find('h')0>>> str6.find('what')12>>> str6.find('.')-1
Index is indexed by element, and the first one is returned without this element error ★★★★★
>>> str6 = 'hello kitty,what can I do4you?'>>> str6.index('h')0>>> str6.index('what')12>>> str6.index('.')Traceback (most recent call last): File "<stdin>", line 1, in <module>ValueError: substring not found
Strip default to remove spaces before and after a string, line breaks, tab ★★★★★
>>> str7 = "\tI'm a superman. ">>> str7.strip()"I'm a superman.">>> str7 = '==hey guys, you shutup!=='>>> str7.strip('==')'hey guys, you shutup!'
Lstrip () Rstrip ()
lstrip()是去除左边的。rstrip()是去除右边的。
Split () splits the string into a list (str---> List), which is separated by a space by default and can specify characters. ★★★★★
>>> str8 = 'spring summer autumn winter'>>> str8.split()['spring', 'summer', 'autumn', 'winter']>>> str8 = 'spring,summer,autumn,winter'>>> str8.split(',')['spring', 'summer', 'autumn', 'winter']
split face question★★★★★
一般有n个分割符,就有n+1个列表元素。但有个特例:当字符串开头有一个空格时并不指定分割符,就只会有n个列表元素。>>> str8 = ' spring summer autumn winter'>>> str8.split()['spring', 'summer', 'autumn', 'winter']>>> str8.split(' ')['', 'spring', 'summer', 'autumn', 'winter']
Split ([Sep [, Maxsplit]]) to set the number of splits.
>>> str8.split(',', 1)['spring', 'summer,autumn,winter']可以用在分割路径与文件名: ★★★★★>>> file_path = r'E:\aaa\bbb\ccc.ddd'>>> path, file = file_path.rsplit('\\', 1)>>> path'E:\\aaa\\bbb'>>> file'ccc.ddd'
Join custom connector to connect elements in an iterative object ★★★★★
>>> s1 = 'abc'>>> '_'.join(s1)'a_b_c'
Replace replacement string, you can specify the number of substitutions ★★★★★
>>> s2 = "Jack is Mary's boy friend.">>> s2.replace('Jack', 'Mark')"Mark is Mary's boy friend.">>> s2.replace('a', 'o', 1)"Jock is Mary's boy friend."
Formatted output: Format
s1 = '我叫{},今年{},性别{}'三种方式第一种s2 = '我叫{},今年{},性别{}'.format('太白','28','男')print(s2)第二种s3 = '我叫{0},今年{1},性别{2},我依然叫{0}'.format('太白', '28', '男')print(s3)第三种s4 = '我叫{name},今年{age},性别{sex}'.format(age='28', name='太白', sex='男')print(s4)
Is series
name = 'taibai'name1 = 'a123'print(name.isalnum()) # 数字或字母组成print(name1.isdigit()) # 判断全部是由整数组成print(name.isalpha()) # 全部由字母组成
Public methods
>>> name='alexaaaa'>>> name.count('a') #可以设置起始位置,结束位置。5>>> print(len(name))8
Python basic Three