如何在字符串中间剪切几个字符? (Python 2.7.8)

时间:2014-11-26 23:00:54

标签: python-2.7

我需要取一个6位有效数字并将其设为3位有效数字,但字符串的结构如下:

string1 = 1.00466E + 15

我需要

string2 = 1.00E + 15

如何从该字符串中删除第5,第6和第7个字符?

1 个答案:

答案 0 :(得分:1)

string2 = string1[:4] + string1[7:]

请参阅:https://docs.python.org/3/tutorial/introduction.html#strings - 特别是'切片'