python's string type

1, the role of

2, the definition of

msg='hello' # msg=str('msg')
print(type(msg))

3, type conversion

str can be any other type strings are transformed into

res=str({'a':1})
print(res,type(res))

4, using: built-in methods

4.1 Priority grasp

4.1.1, according to the index value (forward + reverse take take): can only take

msg='hello world'

Take forward

print(msg[0])
print(msg[5])

Reverse Take

print(msg[-1])

Can only take

msg[0]='H'

4.1.2 Slice: expand the application index, a copy of a substring from a string of large

msg='hello world'

Care regardless of the end

res=msg[0:5] #x
print(res)
print(msg)

Steps

res=msg[0:5:2] # 0 2 4
print(res) # hlo

Reverse steps (understanding)

res=msg[5:0:-1]
print(res) #" olle"

msg='hello world'
res=msg[:] # res=msg[0:11]
print(res)

res=msg[::-1] # 把字符串倒过来
print(res)

4.1.3 length len

msg='hello world'
print(len(msg))

4.1.4, members of the operations in and not in

Determining whether there is a sub-string within a string of large

print("alex" in "alex is sb")
print("alex" not in "alex is sb")
print(not "alex" in "alex is sb") # 不推荐使用

4.1.5, left and right sides of the symbol string is removed strip

Remove the default space

msg='      egon      '
res=msg.strip()
print(msg) # 不会改变原值
print(res) # 是产生了新值

Remove the default space

msg='****egon****'
print(msg.strip('*'))

Learn: strip only take on both sides, not the intermediate

msg='****e*****gon****'
print(msg.strip('*'))

msg='**/*=-**egon**-=()**'
print(msg.strip('*/-=()'))

application

inp_user=input('your name>>: ').strip() # inp_user=" egon"
inp_pwd=input('your password>>: ').strip()
if inp_user == 'egon' and inp_pwd == '123':
    print('登录成功')
else:
    print('账号密码错误')

4.1.6, segmentation split: a string to be segmented according to some separators, give a list of

The default separator is a space

info='egon 18 male'
res=info.split()
print(res)

Specify the delimiter

info='egon:18:male'
res=info.split(':')
print(res)

Specifies the number of separated (understand)

info='egon:18:male'
res=info.split(':',1)
print(res)

4.1.7, cycling

info='egon:18:male'
for x in info:
    print(x)

4.2 need to know

4.2.1、strip,lstrip,rstrip

msg='***egon****'
print(msg.strip('*'))
print(msg.lstrip('*'))
print(msg.rstrip('*'))

4.2.2、lower,upper

msg='AbbbCCCC'
print(msg.lower())
print(msg.upper())

4.2.3、startswith,endswith

print("alex is sb".startswith("alex"))
print("alex is sb".endswith('sb'))

4.2.4、format

4.2.5, split, rsplit: cut the string list

info="egon:18:male"
print(info.split(':',1)) # ["egon","18:male"]
print(info.rsplit(':',1)) # ["egon:18","male"]

4.2.6, join: the list spliced ​​into a string

l=['egon', '18', 'male']
res=l[0]+":"+l[1]+":"+l[2]
res=":".join(l) # 按照某个分隔符号,把元素全为字符串的列表拼接成一个大字符串
print(res)

l=[1,"2",'aaa']
":".join(l)

4.2.7、replace

msg="you can you up no can no bb"
print(msg.replace("you","YOU",))
print(msg.replace("you","YOU",1))

4.2.8、isdigit

Determining whether the string of digits pure

print('123'.isdigit())
print('12.3'.isdigit())

age=input('请输入你的年龄:').strip()
if age.isdigit():
    age=int(age) # int("abbab")
    if age > 18:
        print('猜大了')
    elif age < 18:
        print('猜小了')
    else:
        print('才最了')
else:
    print('必须输入数字,傻子')

Learn 4.3

4.3.1、find,rfind,index,rindex,count

msg='hello egon hahaha'

Find Returns the start index

print(msg.find('e')) # 返回要查找的字符串在大字符串中的起始索引
print(msg.find('egon'))
print(msg.index('e'))

print(msg.index('egon'))

Can not find

print(msg.find('xxx')) # 返回-1,代表找不到
print(msg.index('xxx')) # 抛出异常

msg='hello egon hahaha egon、 egon'
print(msg.count('egon'))

4.3.2, center, light, rjust, zfill

print('egon'.center(50,'*'))
print('egon'.ljust(50,'*'))
print('egon'.rjust(50,'*'))
print('egon'.zfill(10))

4.3.3、expandtabs

msg='hello\tworld'
print(msg.expandtabs(2)) # 设置制表符代表的空格数为2

4.3.4, captalize, swapcase, title

print("hello world egon".capitalize())
print("Hello WorLd EGon".swapcase())
print("hello world egon".title())

4.3.5, is digital series

4.3.6, is the other

print('abc'.islower())
print('ABC'.isupper())
print('Hello World'.istitle())
print('123123aadsf'.isalnum()) # 字符串由字母或数字组成结果为True
print('ad'.isalpha()) # 字符串由由字母组成结果为True
print('     '.isspace()) # 字符串由空格组成结果为True
print('print'.isidentifier())
print('age_of_egon'.isidentifier())
print('1age_of_egon'.isidentifier())

num1=b'4' #bytes
num2=u'4' #unicode,python3中无需加u就是unicode
num3='四' #中文数字
num4='Ⅳ' #罗马数字

isdigit只能识别:num1、num2
print(num1.isdigit()) # True
print(num2.isdigit()) # True
print(num3.isdigit()) # False
print(num4.isdigit()) # False

isnumberic可以识别:num2、num3、num4
print(num2.isnumeric()) # True
print(num3.isnumeric()) # True
print(num4.isnumeric()) # True

isdecimal只能识别:num2
print(num2.isdecimal()) # True
print(num3.isdecimal()) # False
print(num4.isdecimal()) # False

Guess you like

Origin www.cnblogs.com/Lance-WJ/p/12456642.html