在Python中修改字符串

rec*_*gle 2 python string

我在python'#b9d9ff'中有一个字符串.如何删除哈希符号(#)?

Joh*_*ica 8

有各种不同的选择.每个人对你的字符串做同样的事情,但不同地处理其他字符串.

# Strip any hashes on the left.
string.lstrip('#')

# Remove hashes anywhere in the string, not necessarily just from the front.
string.replace('#', '')

# Remove only the first hash in the string.
string.replace('#', '', 1)

# Unconditionally remove the first character, no matter what it is.
string[1:]

# If the first character is a hash, remove it. Otherwise do nothing.
import re
re.sub('^#', '', string)
Run Code Online (Sandbox Code Playgroud)

(如果你不关心哪个,请使用lstrip('#').这是最自我描述的.)