Python中是否有任何函数可用于在字符串的某个位置插入值?
像这样的东西:
"3655879ACB6"
然后在位置4添加 "-"
变成 "3655-879ACB6"
原文由 Michel Andrade 发布,翻译遵循 CC BY-SA 4.0 许可协议
Python中是否有任何函数可用于在字符串的某个位置插入值?
像这样的东西:
"3655879ACB6"
然后在位置4添加 "-"
变成 "3655-879ACB6"
原文由 Michel Andrade 发布,翻译遵循 CC BY-SA 4.0 许可协议
这看起来很简单:
>>> hash = "355879ACB6"
>>> hash = hash[:4] + '-' + hash[4:]
>>> print hash
3558-79ACB6
但是,如果您喜欢函数之类的东西,请执行以下操作:
def insert_dash(string, index):
return string[:index] + '-' + string[index:]
print insert_dash("355879ACB6", 5)
原文由 Mario César 发布,翻译遵循 CC BY-SA 2.5 许可协议
2 回答5.2k 阅读✓ 已解决
2 回答1.1k 阅读✓ 已解决
4 回答1.4k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
1 回答2.8k 阅读✓ 已解决
2 回答862 阅读✓ 已解决
不,Python 字符串是不可变的。
但是,可以创建一个包含插入字符的新字符串: