Python 文字列内の指定位置の文字を削除
文字列を指定し、指定位置の文字を削除します:
例
test_str = "サンプル"
# 元の文字列を出力
print ("元の文字列 : " + test_str)
# 3番目の文字 n を削除
new_str = ""
for i in range(0, len(test_str)):
if i != 2:
new_str = new_str + test_str[i]
print ("文字列削除後 : " + new_str)
# 元の文字列を出力
print ("元の文字列 : " + test_str)
# 3番目の文字 n を削除
new_str = ""
for i in range(0, len(test_str)):
if i != 2:
new_str = new_str + test_str[i]
print ("文字列削除後 : " + new_str)
上記のコードを実行すると、出力結果は次のとおりです:
原始字符串为 : サンプル 字符串移除后为 : Ruoobその他の拡張
Python3 例