No.031 [Python] 문자열의 분할 ②
3449 단어 Pythonprogramming
1초에 한 번씩 문자열 분할에 대해 적어 보세요.
I'll write about the split of strings in python"on this page a little.
■ 문자열 목록의 링크
Link between sting lists
>>> # 文字列のリストを一つの文字列へ連結:文字列メソッドjoin()
>>> # 1.'挿入する文字列'でjoin()メソッドを呼び出す
>>> # 2. 連結する文字列のリストを引数として渡す
>>>
>>> l = ["one", "two","three"]
>>>
>>> print(",".join(l))
one,two,three
>>>
>>> print("\n".join(l))
one
two
three
>>>
>>> print(" ".join(l))
one two three
>>>
>>>
>>> # joinを使わなくても同じ表示結果となる
>>>
>>> l = ["one", "two","three"]
>>> print(*l, sep=',')
one,two,three
>>> print(*l, sep='\n')
one
two
three
>>> print(*l)
one two three
■자수 분할: 슬라이스
Division by the number of characters: Slice
>>> # 文字数による分割の場合は、Sliceを使用する
>>>
>>> s = 'abcdefg'
>>>
>>> print(s[:3])
abc
>>>
>>> print(s[3:])
defg
>>> # タプルとして取得、また変数に代入も可能
>>>
>>> s_tuple = s[:3], s[3:]
>>>
>>> print(s_tuple)
('abc', 'defg')
>>>
>>> print(type(s_tuple))
<class 'tuple'>
>>>
>>> s_first, s_last = s[:3], s[3:]
>>>
>>> print(s_first)
abc
>>>
>>> print(s_last)
defg
>>> # 3分割することの可能
>>>
>>> s = 'abcdefghij'
>>>
>>> s_first, s_second, s_last = s[:3], s[3:6],s[6:]
>>>
>>> print(s_first)
abc
>>> print(s_second)
def
>>> print(s_last)
ghij
>>> # 文字数は、組み込み関数len()で取得可能、半分ずつに分割可能
>>>
>>> s = 'abcdefghij'
>>>
>>> half = len(s) // 2
>>>
>>> print(half)
5
>>>
>>> s_first, s_last = s[:half], s[half:]
>>>
>>> print(s_first)
abcde
>>>
>>> print(s_last)
fghij
>>> # +演算子にて連結が可能
>>>
>>> s = 'abcdefghij'
>>>
>>> print(half)
5
>>> s_first, s_last = s[:half], s[half:]
>>>
>>> print(s_first + s_last)
abcdefghij
>>> # 負数や大きな数を指定した場合
>>>
>>> "abcdefg"[-3:]
'efg'
>>> "abcdefg"[-99:]
'abcdefg'
>>> "abcdefg"[99:]
''
>>> "abcdefg"[:99]
'abcdefg'
>>>
>>>
>>> # スキップ指定の場合
>>>
>>> "abcdefg"[::2]
'aceg'
>>> "abcdefg"[1::2]
'bdf'
어때요?How was my post?
본 보도는 수시로 업데이트될 것이니 정기적으로 구독해 주십시오.
I'll update my blogs at all times.
So, please subscribe my blogs from now on.
본 보도에 관하여 만약 무슨 요구가 있으면 마음대로 메시지를 남겨 주십시오!
If you have some requests, please leave some messages! by You-Tarin
또한 Qita에 투고한 내용은 언제든지 블로그에 가고 싶으니 잘 부탁드립니다.
Reference
이 문제에 관하여(No.031 [Python] 문자열의 분할 ②), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/You-Tarin/items/92c1b5467c5eebe01ea1텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)