1.数値、文字列

19098 ワード

ディジタルタイプ


Xのy二乗を表す**演算子
>>a=3
>>b=4
>>a+b
81
除算後に残りの%演算子を返します
>>7%3
1
除算後にシェアを返す//演算子
>>7//4
1
実数型
4.24E10 = 4.2410**10
4.24e-10=4.2410**-10

文字列


文字列の作成と使用方法


連続して3つの引用符(")または単一引用符(")を使用します.
"""Life is too short, you need to python"""
'''Life is too short, you need to python'''
文字列に一重引用符を含める
1.큰따옴표로 둘러싸기
"Python's favorite food is perl"
2. 앞에 백슬러시 삽입
  'Python\'s favorite food is perl'
文字列に二重引用符を含むタイムキー
1.작은 따옴표로 둘러싸기
'"she is smart." he says.'
앞에 백슬러시 삽입
"\"she is smart." he says."
折り返し
1.エスケープコード「がn」
multiline= "Life is too short\n you need to python"
2.연속한 작은 따옴표나 큰 따옴표 3개 사용
multiline='''
···Life is too short
···you need to python
'''
multiline="""
···Life is too short
···you need to python
"""

計算文字列


文字列の長さを求める
>>a="Life is too short"
>>len(a)
17

文字列の索引付け


数値にスペースを含める
0,1,....9、0、1、2に数字が与えられる

後ろの最初の文字
>>a="Life is too short, you need to python"
>>a[-1]
n
スライドを使用して文全体を出力
>>a[:]
"Life is too short, you need to python"
[スムーズ](Smooth)マイナス記号も使用できます
>>a[19:-7]
'you need'

文字列マッピング


:文字列内の特定の値を置換します。


1)模倣文字列マッピング
1.숫자 바로 대입
>>" I eat %d apples." %3
'I eat 3 apples.'
2. 문자열 바로 대입
>> " I eat %s apples." %"five"
'I eat five apples.'
3.숫자 값을 나타내는 변수로 대입
>>number=3
>>" I eat %d apples." % number
' I eat 3 apples.'
4.2개 이상의 값 넣기
>> number=10
>> day= "three"
>>" I ate %d apples. so I was sick %s days." %(number, day)
'I ate 10 apples. so I was sick three days.
%sは、%の後に値を文字列に自動的に置き換えます.
2)書式コードを数字と一緒に使う
%10 s:10文字列領域の値を右揃え
%-10 s:左
>>"%10s" % "hi"
'        hi'
>>"%-10sjane" % "hi"
hi        jane
小数点を表す
>>"%0.4f" % 3.42134521
'3.4213'
#3.42134521를 소수 4자리까지 나타내는 경우
>>%10.4f" % 3.42134521
'    3.4213'
# 3.42134521의 소수 4번째 자리까지 + 전체 길이가 10개인 문자열로 표현

3)format関数によるマッピング


直接代入数
>>" I eat {0} apples.".format(3)
' I eat 3 apples.'
直接置換文字列
>>" I eat {0} apples.".format("five")
'I eat five apples.'
変数を数値で置換
>>number=3
>>" I eat {0} apples.".format(number)
' I eat 3 apples.'
複数の値を入力
>> number=10
>> day= "three"
>>" I ate {0} apples. so I was sick {1} days.".format(number, day)
'I ate 10 apples. so I was sick three days.
名前を付ける
>>" I ate {number} apples. so I was sick {day} days.".format(number=10, day=3)
'I ate 10 apples. so I was sick three days.
インデックスと名前のブレンド
" I ate {0} apples. so I was sick {day} days.".format(10, day=3)
'I ate 10 apples. so I was sick three days.
左揃え
>>"{0:<10}".format("hi")
'hi        ' # 총 자릿수 10개
右揃え
>>"{0:>10}".format("hi")
'        hi'
中央揃え
"{0:^10}".format("hi")
'    hi    '
空白を埋める
>>"{0:=^10}.format("hi")
'====hi===='
>>"{0:!<10}.format("hi")
'hi!!!!!!!!'
小数点を表す
>>y=3.42134521
>>"{0:0.4f}".format(y)
'3.4213'
>>"{10.4f}.format(y)
'    3.4213' 
表示文字{}
>>"{{and}}".format
'{and}'

f文字列マッピングは3.6バージョンから始まります。p 65-67を参照してください。


文字列関連関数


文字数:count
>> a="hobby"
>> a.count('b')
2
位置2:を教えます.index()
>>a = "I eat 3 apples."
>> a.index('I')
0
挿入文字列(join)
>>",".join('abcd')
'a,b,c,d'
左スペースをクリア:lstrip
>>a="  hi  "
>>a.lstrip()
'hi  '
置換文字列ちかんもじ:置換ちかん
>>a= "Life is too short"
>>a.replace("Life","your leg")
'Your leg is too short'
文字列分割:split
>>a= "Life is too short"
>>a.split() #공백으로 나누기
['Life', 'is', 'too', 'short']
>>b="a:b:C:D"
>>b.split(:) # ':'기준으로 나누기
['a','b','c','d']
位置1:を教えます.find('')
大文字小文字:.upper()
小文字で:.lower()
右側のスペースをクリア:rstrip()
両側のスペースをクリア:strip()
出典:「Doit!JumptoPython」