0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python 文字列に特定文字が含まれていたら削除する

Last updated at Posted at 2021-07-07

やりたいこと

・”Python” という文字列に、"a","i","u","e","o"のいずれかの文字が含まれていたら
 削除して、出力する。

実地

例1)aiueoではない文字のみを、リストに追加

mozi = "Python"
target = "aiueo"

l = [] #空リストの作成

for s in mozi:
  if s not in target:
    l.append(s) 
   
print(''.join(l))
# Pythn と出力される

例2)↓こっちの表記の方が、リスト作成不要で、よりスマート。

mozi = "Python"
target = "aiueo"

n = ''.join(s for s in mozi if s not in target)
print(n)
# Pythn と出力される

(補足) ''.join(リスト)

'間に挿入する文字'.join(リスト)で
リストの文字列を、指定した文字で繋げて表示できる。
''の場合は、区切り文字なしで表示。

例)

l = ["123", "aaa", "456"]

print(''.join(l))
# 123aaa456 と出力される

print('-'.join(l))
# 123-aaa-456 と出力される

print('pop'.join(l))
# 123popaaapop456 と出力される

print('\n'.join(l))
# 123
# aaa
# 456
# 改行されて表示される
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?