内置方法

字符串的内置方法:

    移除空白strip

mas='         hello        '

print(msg)

print(msg.strip())

rstrip移除右边空白

lstrip移除左边空白

用法:

while True:

       name=input('user: ').strip

       pwd=input('password').strip

       if name=='egon' and pwd=='123':

              print('successful')

切分split

     info='root:x:0:0::/root:bin/bash'

     print(info[0]+info[1]+info[2]+info[3])

     user_1=info.split(':')

     print(user_1[10])

索引

长度(len)

print(len('hello 123'))

切片:切除字符串

msg='hello world'

print(msg[1:3])#1 2

print(msg[1:4])#1 2 3

今天还学到了

startswith #开头的

endswith#结尾的

replace#替换

lower#全部小写

upper#全部大写

capitalize#首字母大写,其余部分小写

swapcase#大小写翻转

title#每个单词的首字母大写

join#加入

数字的内置方法

int float

long(在py2中采用才有)

num=10

num=int(10)

print(type(num),num)

salary=12.5

salary=float(12.5)

print(type(salary),salary)

复数

x=1-2j

print(x.real)

print(x.imag)

 

  

原文地址:https://www.cnblogs.com/1996-11-01-614lb/p/7207754.html