文字列の結合
word1 = 'abc' word2 = 'def' word3 = word1 + '-' + word2 print(word3) #abc-def
word1 = 'abc' word2 = 'def' word_list = [word1, word2] print('-'.join(word_list)) #abc-def
word1 = 'abc' word2 = 'def' word3 = '{}-{}'.format(word1, word2) print(word3) #abc-def
文字列の切り出し
word = 'abcdefghijk' print(word[3:]) #defghijk print(word[:3]) #abc print(word[1:3]) #bc print(word[:-2]) #abcdefghi
文字列の置換
word = 'a-b-c-d-e' new_word = word.replace('-', ',') print(new_word) #a,b,c,d,e
文字列の分割
word = 'a-b-c-d-e' print(word.split('-')) #['a', 'b', 'c', 'd', 'e']
文字列両端の空白削除
両端のスペース・タブ文字・改行を削除します
print(word.strip()) #'x' word = ' x ' print(word.rstrip()) #' x' print(word.lstrip()) #'x '