Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
次の形式の文字列のリストがあります'foo7bar'。7Python で、後に続く文字と一緒に削除するにはどうすればよいですか?
'foo7bar'
7
これはこの質問に似ていますが、Python の回答が必要です。
これは、Python のスライス表記を使用して行うことができます。
>>> mystr = 'foo7bar' >>> mystr[:mystr.index('7')] 'foo' >>>
スライス表記の形式は[start:stop:step]. 文字列のindexメソッドは、その入力が最初に出現する位置を見つけます。
[start:stop:step]
index
ただし、より複雑なもの (パターンのマッチングなど) を扱っている場合は、正規表現を調べることをお勧めします。ただし、この操作では、スライス表記で十分です。