文字列
38405 ワード
📌 文字列データ型
👉 文字列=引用符で囲む
"python"
"123"
👉 4つの文字列の作成方法
"python"
'python'
"""python"""
'''python'''
👉 引用符を文字列に含める!
cv変数にIt's my Developgを保存
>>> cv = "It's my velog"
say変数では「It's myvelog」と述べた。保存#ホゾン#
>>> say = '"It is my velog", he says.'
スラッシュの使用()
>>> cv = 'It\'s my velog'
>>> say = "\"It is my velog\", he says."
👉 複数行文字列を変数に挿入
Multiline変数で..。保存#ホゾン#
I love music.
My best thing is dusk till dawn.
nエスケープコードを挿入n
>>> multiple = "I love music.\nMy best thing is dusk till dawn."
連続3つの引用符
>>> multiline = """
I love music.
My best thing is dusk till dawn.
"""
👉 計算文字列
プラス記号
>>> head = "Hi, "
>>> tail = "nice to meet you."
>>> head + tail
'Hi, nice to meet you.'
乗算
>>> a = 'python'
>>> a * 2
'pythonpython'
文字列の長さを求める
>>> a = "Hi, nice to meet you."
>>> len(a)
21
👉 索引とスライド
インデックスとは
ななめに切る
索引
"python"
"123"
"python"
'python'
"""python"""
'''python'''
>>> cv = "It's my velog"
>>> say = '"It is my velog", he says.'
>>> cv = 'It\'s my velog'
>>> say = "\"It is my velog\", he says."
I love music.
My best thing is dusk till dawn.
>>> multiple = "I love music.\nMy best thing is dusk till dawn."
>>> multiline = """
I love music.
My best thing is dusk till dawn.
"""
>>> head = "Hi, "
>>> tail = "nice to meet you."
>>> head + tail
'Hi, nice to meet you.'
>>> a = 'python'
>>> a * 2
'pythonpython'
>>> a = "Hi, nice to meet you."
>>> len(a)
21
ゼロから水が漏れる
>>> a = "Hi, nice to meet you."
>>> a[5]
'i'
>>> a[0]
'H'
逆数(-)
>>> a = "Hi, nice to meet you."
>>> a[-1]
'.'
>>> a[-6]
't'
索引
終了番号を含める
>>> a = "Hi, nice to meet you."
>>> a[0:2]
'Hi'
さまざまな例
>>> a = "Hi, nice to meet you."
>>> a[0:2]
'Hi'
>>> a[:2]
'Hi'
>>> a[4:8]
'nice'
>>> a[8:]
' to meet you.'
>>> a[:]
'Hi, nice to meet you.'
>>> a[4:-13]
'nice'
文字列分割
>>> a = "20210529Rainy"
>>> date = a[:8]
>>> weather = a[8:]
>>> date
'20210529'
>>> weather
'Rainy'
👉 文字列マッピング
直接代入数
>>> "I have %d pens." %3
'I have 3 pens.'
文字列の直接代入
>>> "I have %s pens." %"three"
'I have three pens.'
数値を表す変数として入力
>>> number = 3
>>> "I have %d pens." %number
'I have 3 pens.'
2つ以上の値を入力
>>> number = 5
>>> day = "two"
>>> "I have %d pens that i bought %s days ago" %(number, day)
全能%s
>>> "I have %s pens." %5
'I have 5 pens.'
>>>"rate is %s" %90.3
'rate is 90.3'
「%」を含む
>>> "Its possibility is %d%%." %90
'Its possibility is 90%.'
書式コードと数値の使用
配置とスペース
>>> "%10s" %"Hi"
Hi
>>> "%-10sTom" %"Hi"
Hi Tom
小数点を表す
>>> "%0.4f"% 3.141592
'3.1416'
>>> "%10.4f"%3.141592
' 3.1416'
form関数を使用したマッピング
直接代入数
>>> "I have {0} pens".format(3)
'I have 3 pens
直接文字列を挿入
>>> "I have {0} pens".format("three")
'I have three pens'
数値を表す変数として置換
>>> number = 3
>>> "I have {0} pens".format(number)
複数の値を入力
>>> number = 5
>>> day = "two"
>>> "I have {0} pens that i bought {1} days ago".format(number, day)
'I have 5 pens that i bought two days ago
名前を付ける
>>> "I have {number} pens that i bought {day} days ago".format(number = 5, day = "two")
'I have 5 pens that i bought two days ago'
インデックスと名前のブレンド
>>> "I have {0} pens that i bought {day} days ago".format(3, day="two")
'I have 3 pens that i bought two days ago'
ツールバーの
>>> "{0:<10}".format("Hi")
'Hi '
>>> "{0:>10}".format("Hi")
' Hi'
>>>"{0:^10}".format("Hi")
' Hi '
空白を埋める
>>> "{0:=^10}".format("Hi")
'====Hi===='
>>> "{0:#<10}".format("Hi")
'Hi########'
小数点
>>> a = 3.141592
>>> "{0:0.4f}".format(a)
'3.1416'
>>> "{0:10.4f}".format(a)
' 3.1416'
{または}を表す文字
>>> "{{ Hi }}".format()
'{ Hi }'
文字列マッピング
はい。
>>> number = 3
>>> day = "two"
>>> f'I have {number} pens that i bought {day} days ago'
式を使用可能にする
'I have 3 pens that i bought two days ago'
>>> f'I have {number + 1} pens that i bought {day} days ago'
'I have 4 pens that i bought two days ago'
ディックキャンプを使用可能
>>> d = {'number':3, 'day':'two'}
>>> f'I have {d["number"]} pens that i bought {d["day"]} days ago'
'I have 3 pens that i bought two days ago'
ツールバーの
>>> f'{"Hi":<10}'
'Hi '
>>> f'{"Hi":>10}'
' Hi'
>>> f'{"Hi":^10}'
' Hi '
空白を埋める
>>> f'{"Hi":=^10}'
'====Hi===='
>>> f'{"Hi":#<10}'
'Hi########'
小数点
>>> a = 3.141592
>>> f'{a:0.4f}'
'3.1416'
>>> f'{a:10.4f}'
' 3.1416'
{または}を表す文字
>>> f'{{ Hi }}'
'{ Hi }'
👉 文字列関連関数(8個)
文字列データ型には、独自の関数があります.文字列変数の後の"."プラスして、関数名を書けばいいです.
文字数世紀
>>> a = "bubble"
>>> a.count('b')
3
ヒント位置1(find)
>>> a = "I have 3 pens that i bought two days ago"
>>> a.find('s')
12
>>> a.find('c')
-1
ヒント位置2(index)
>>> a = "I have 3 pens that i bought two days ago"
>>> a.index('s')
12
>>> a.index('c')
Traceback (most recent call last):
File "main.py", line 1, in <module>
print(a.index('c'))
ValueError: substring not found
挿入文字列(join)
この関数は、文字列だけでなく、リストや凡例も入力できます.
>>> ",".join('ABCDE')
A,B,C,D,E
大文字と小文字の変更
>>> a = "hi"
>>> a.upper()
'HI'
>>> b = "HI"
>>> b.lower()
'hi'
空白をクリア(lstrip、rstrip、strip)
>>> a = " Hi "
>>> a.lstrip()
'Hi '
>>> a.rstrip()
' Hi'
>>> a.strip()
'Hi'
置換文字列
>>> a = "My favorite food is pizza"
>>> a.replace('pizza','chicken')
'My favorite food is chicken'
区切り文字列(split)
カッコに何もない場合は、文字列をスペース(スペース、タブ、エンティティなど)で区切ります.
>>> a = "My favorite food is pizza"
>>> a.split()
['My' ,'favorite' ,'food' ,'is' ,'pizza']
>>> b = "a;b;c;d"
>>> b.split(";")
['a', 'b', 'c', 'd']
文字列の値は変更できません
>>> a = pithon
>>> a[1] = 'y'
Traceback (most recent call last):
File "main.py", line 2, in <module>
a[1] = "y"
TypeError: 'str' object does not support item assignment
❗文字列フォーマットコード
コード摘要
%s
文字列%c
1文字%d
整数%f
浮動小数点数%o
8進%x
16進%%
テキスト%(文字%自体)100 findとindexの違い
findとindexは、文字列に最初に現れる位置を返します.ただし、検索された文字または文字列が存在しない場合、findは-1、indexはエラーを引き起こします.
Reference
この問題について(文字列), 我々は、より多くの情報をここで見つけました https://velog.io/@kj_min/자료형문자열テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol