Pythonの文字列型

1、の役割

2、の定義

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

図3に示すように、型変換

strが文字列がに変換されている他のタイプとすることができます

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

4、使用:組み込みメソッド

4.1優先順位の把握

4.1.1、インデックス値(フォワード+逆テイクテイク)によればのみ取ることができます

msg='hello world'

前方ください

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

リバーステイク

print(msg[-1])

だけ取ることができます

msg[0]='H'

4.1.2スライス:アプリケーションインデックス、大型の文字列から部分のコピーを拡大

msg='hello world'

かかわらず、最後のケア

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

ステップ

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

逆の手順(理解)

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長さLEN

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

4.1.4、業務のメンバーではありませんで

大型の文字列内のサブストリングがあるかどうかを決定します

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

4.1.5、左と記号列の右側がストリップを取り除かれます

デフォルトのスペースを削除します

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

デフォルトのスペースを削除します

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

学習:ストリップは中間、両側ではない取ります

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

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

アプリケーション

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、セグメンテーションスプリット:いくつかの区切りに応じてセグメント化する文字列のリストを与えます

デフォルトの区切り文字はスペースです

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

区切り文字を指定します。

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

分離の数(理解)を指定します

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

4.1.7、サイクリング

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

知る必要性4.2

4.2.1、ストリップ、lstrip、rstrip

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

4.2.2、上下

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、スプリット、rsplit:カット文字列リスト

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

4.2.6、参加:文字列内にスプライシングリストを

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、交換してください

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

4.2.8、isdigit

純粋な数字の文字列かどうかを判断します

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('必须输入数字,傻子')

4.3学びます

4.3.1、検索、RFIND、インデックス、RINDEX、カウント数

msg='hello egon hahaha'

返しに開始インデックスを探します

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

print(msg.index('egon'))

見つけることができません

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

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

4.3.2、センター、光、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、タイトル

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

4.3.5、デジタルシリーズです

4.3.6、他のです

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

おすすめ

転載: www.cnblogs.com/Lance-WJ/p/12456642.html