記事のディレクトリ
コース:文字列
目標
- 文字列を認識する
- 添字
- スライス
- 一般的な操作方法
1.文字列を認識する
文字列は、Pythonで最も一般的に使用されるデータ型です。通常、文字列の作成には引用符を使用します。文字列の作成は簡単で、変数に値を割り当てるだけです。
a = 'hello world'
b = "abcdefg"
print(type(a))
print(type(b))
注:コンソールに表示される結果は
<class 'str'>
、つまり、データ型はstr(文字列)です。
1.1文字列の特性
- 引用符で囲まれた文字列のペア
name1 = 'Tom'
name2 = "Rose"
- 三重引用符で囲まれた文字列
name3 = ''' Tom '''
name4 = """ Rose """
a = ''' i am Tom,
nice to meet you! '''
b = """ i am Rose,
nice to meet you! """
注:三重引用符の形式の文字列は、改行をサポートします。
思考:文字列を作成する方法は
I'm Tom
?
c = "I'm Tom"
d = 'I\'m Tom'
1.2文字列出力
print('hello world')
name = 'Tom'
print('我的名字是%s' % name)
print(f'我的名字是{name}')
1.3文字列入力
Pythonでは、を使用してinput()
ユーザー入力を受け取ります。
- コード
name = input('请输入您的名字:')
print(f'您输入的名字是{name}')
print(type(name))
password = input('请输入您的密码:')
print(f'您输入的密码是{password}')
print(type(password))
- 出力結果
第二に、下付き文字
“下标”
とも呼ばれ“索引”
、それは数です。たとえば、列車の座席番号、座席番号の役割:番号に従って対応する座席をすばやく見つけます。同様に、下付き文字の機能は、下付き文字を介して対応するデータをすばやく見つけることです。
2.1クイックエクスペリエンス
要件:文字列name = "abcdef"
、さまざまな添え字に対応するデータを取得します。
- コード
name = "abcdef"
print(name[1])
print(name[0])
print(name[2])
- 出力結果
注:下付き文字は0ベギン。
三、スライス
スライスとは、操作の対象の一部を傍受する操作のことです。文字列、リスト、およびタプルはすべて、スライス操作をサポートしています。
3.1構文
序列[开始位置下标:结束位置下标:步长]
注意
1. 不包含结束位置下标对应的数据, 正负整数均可;
2. 步长是选取间隔,正负整数均可,默认步长为1。
3.2経験
name = "abcdefg"
print(name[2:5:1]) # cde
print(name[2:5:2]) # ce 注意步长变化
print(name[2:5]) # cde
print(name[:5]) # abcde
print(name[1:]) # bcdefg
print(name[:]) # abcdefg
print(name[::2]) # aceg
print(name[:-1]) # abcdef, 负1表示倒数第一个数据
print(name[-4:-1]) # def
print(name[::-1]) # gfedcba
4つの一般的な操作方法
文字列の一般的に使用される操作方法は、検索、変更、判断の3つのカテゴリに分類されます。
4.1検索
いわゆる文字列検索方法は、文字列内の部分文字列の位置または出現回数を見つけることです。
- find():特定の部分文字列がこの文字列に含まれているかどうかを検出します。部分文字列の先頭に添え字を返す場合は、-1を返します。
- 文法
字符串序列.find(子串, 开始位置下标, 结束位置下标)
注:開始位置と終了位置の添え字は省略できます。これは、文字列シーケンス全体を検索することを意味します。
- クイックエクスペリエンス
mystr = "hello world and itcast and itheima and Python"
print(mystr.find('and')) # 12
print(mystr.find('and', 15, 30)) # 23
print(mystr.find('ands')) # -1
- index():特定の部分文字列がこの文字列に含まれているかどうかを検出します。この部分文字列の先頭にインデックスを返す場合は、例外を報告します。
- 文法
字符串序列.index(子串, 开始位置下标, 结束位置下标)
注:開始位置と終了位置の添え字は省略できます。これは、文字列シーケンス全体を検索することを意味します。
- クイックエクスペリエンス
mystr = "hello world and itcast and itheima and Python"
print(mystr.index('and')) # 12
print(mystr.index('and', 15, 30)) # 23
print(mystr.index('ands')) # 报错
- rfind():find()と同じ関数ですが、検索方向は正しいベギン。
- rindex():index()と同じ関数ですが、検索方向は正しいベギン。
- count():特定の部分文字列が文字列に出現する回数を返します
- 文法
字符串序列.count(子串, 开始位置下标, 结束位置下标)
注:開始位置と終了位置の添え字は省略できます。これは、文字列シーケンス全体を検索することを意味します。
- クイックエクスペリエンス
mystr = "hello world and itcast and itheima and Python"
print(mystr.count('and')) # 3
print(mystr.count('ands')) # 0
print(mystr.count('and', 0, 20)) # 1
4.2変更
いわゆる文字列の変更とは、関数の形式で文字列内のデータを変更することを指します。
- replace():置換
- 文法
字符串序列.replace(旧子串, 新子串, 替换次数)
注:部分文字列の出現回数が検出された場合、置換の数は部分文字列の出現回数になります。
- クイックエクスペリエンス
mystr = "hello world and itcast and itheima and Python"
# 结果:hello world he itcast he itheima he Python
print(mystr.replace('and', 'he'))
# 结果:hello world he itcast he itheima he Python
print(mystr.replace('and', 'he', 10))
# 结果:hello world and itcast and itheima and Python
print(mystr)
注:データは、直接変更できるかどうかに分けられます可変型と不変型2種類。文字列型のデータを変更した場合、元の文字列は変更できません。データを直接変更できない型は不変型です。
- split():指定された文字に従って文字列を分割します。
- 文法
字符串序列.split(分割字符, num)
注:numは分割文字の出現回数を表し、返されるデータの数はnum +1になります。
- クイックエクスペリエンス
mystr = "hello world and itcast and itheima and Python"
# 结果:['hello world ', ' itcast ', ' itheima ', ' Python']
print(mystr.split('and'))
# 结果:<class 'list'>
print(type(mystr.split('and')))
# 结果:['hello world ', ' itcast ', ' itheima and Python']
print(mystr.split('and', 2))
# 结果:['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
print(mystr.split(' '))
# 结果:['hello', 'world', 'and itcast and itheima and Python']
print(mystr.split(' ', 2))
注:分割文字が元の文字列の部分文字列である場合、分割後に部分文字列は失われます。
- join():文字列を1つの文字または部分文字列と結合します。つまり、複数の文字列を結合して新しい文字列にします。
- 文法
字符或子串.join(多字符串组成的序列)
- クイックエクスペリエンス
list1 = ['chuan', 'zhi', 'bo', 'ke']
t1 = ('aa', 'b', 'cc', 'ddd')
# 结果:chuan_zhi_bo_ke
print('_'.join(list1))
# 结果:aa...b...cc...ddd
print('...'.join(t1))
- Capitalize():文字列の最初の文字を大文字に変換します。
mystr = "hello world and itcast and itheima and Python"
# 结果:Hello world and itcast and itheima and python
print(mystr.capitalize())
注:capitalize()関数が変換された後、文字列の最初の文字のみが大文字になり、他の文字はすべて小文字になります。
- title():文字列内の各単語の最初の文字を大文字に変換します。
mystr = "hello world and itcast and itheima and Python"
# 结果:Hello World And Itcast And Itheima And Python
print(mystr.title())
- lower():文字列内で大文字を小文字に変換します。
mystr = "hello world and itcast and itheima and Python"
# 结果:hello world and itcast and itheima and python
print(mystr.lower())
- upper():文字列内で小文字を大文字に変換します。
mystr = "hello world and itcast and itheima and Python"
# 结果:HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON
print(mystr.upper())
- lstrip():文字列の左側にある空白文字を削除します。
- rstrip():文字列の右側の空白文字を削除します。
- strip():文字列の両側の空白文字を削除します。
- ljust():元の文字列を左揃えにし、指定された文字(デフォルトのスペース)で対応する長さに埋めた新しい文字列を返します。
- 文法
字符串序列.ljust(长度, 填充字符)
- 出力効果
- rjust():元の文字列を右寄せし、指定された文字(デフォルトのスペース)で対応する長さに埋めた新しい文字列を返します。構文はljust()と同じです。
- center():元の文字列を中央に揃え、指定された文字(デフォルトのスペース)で対応する長さに埋めた新しい文字列を返します。構文はljust()と同じです。
4.3判断
いわゆる判断はtrueまたはfalseを判断することであり、返される結果はブールデータ型(TrueまたはFalse)です。
- startupwith():文字列が指定された部分文字列で始まるかどうかを確認します。開始する場合はTrueを返し、そうでない場合はFalseを返します。開始位置と終了位置の添え字を設定する場合は、指定した範囲内で確認してください。
- 文法
字符串序列.startswith(子串, 开始位置下标, 结束位置下标)
- クイックエクスペリエンス
mystr = "hello world and itcast and itheima and Python "
# 结果:True
print(mystr.startswith('hello'))
# 结果False
print(mystr.startswith('hello', 5, 20))
- extendswith():文字列が指定された部分文字列で終了するかどうかを確認します。終了する場合はTrueを返し、そうでない場合はFalseを返します。開始位置と終了位置の添え字を設定する場合は、指定した範囲内で確認してください。
- 文法
字符串序列.endswith(子串, 开始位置下标, 结束位置下标)
- クイックエクスペリエンス
mystr = "hello world and itcast and itheima and Python"
# 结果:True
print(mystr.endswith('Python'))
# 结果:False
print(mystr.endswith('python')) # 注意首字母大小写
# 结果:False
print(mystr.endswith('Python', 2, 20))
- isalpha():文字列に少なくとも1文字があり、すべての文字が文字の場合はTrueを返し、それ以外の場合はFalseを返します。
mystr1 = 'hello'
mystr2 = 'hello12345'
# 结果:True
print(mystr1.isalpha())
# 结果:False
print(mystr2.isalpha())
- isdigit():文字列に数字のみが含まれている場合はTrueを返し、それ以外の場合はFalseを返します。
mystr1 = 'aaa12345'
mystr2 = '12345'
# 结果: False
print(mystr1.isdigit())
# 结果:True
print(mystr2.isdigit())
- isalnum():文字列に少なくとも1文字があり、すべての文字が文字または数字の場合はTrueを返し、そうでない場合はFalseを返します。
mystr1 = 'aaa12345'
mystr2 = '12345-'
# 结果:True
print(mystr1.isalnum())
# 结果:False
print(mystr2.isalnum())
- isspace():文字列に空白のみが含まれている場合はTrueを返し、そうでない場合はFalseを返します。
mystr1 = '1 2 3 4 5'
mystr2 = ' '
# 结果:False
print(mystr1.isspace())
# 结果:True
print(mystr2.isspace())
V.まとめ
- 添字
- コンピューターは、データシーケンスの各要素に0から始まる番号を割り当てます
- スライス
序列名[开始位置下标:结束位置下标:步长]
- 一般的な操作方法
- find()
- index()
このセクションの内容は終わりました。迷わないように注意してください。