字符串复制和粘贴

时间:2014-09-18 05:12:54

标签: python

我有一个像1:sometext:othertext这样的字符串,我必须在第二个冒号之后复制该部分并将其粘贴到它的末尾,1:sometext:othertext othertext

有人可以帮助我吗?

4 个答案:

答案 0 :(得分:1)

试试这个

str1 = "hello:hi:world"
x, y, str2 = str1.split(":")
str1 = str1 + str2 # if you need space then str1 = str1+" "+str2
print(str1)

答案 1 :(得分:0)

您可以使用split函数中内置的字符串来执行此操作。

text = '1:sometext:othertext'
text + ' ' + text.split(':')[2]

答案 2 :(得分:0)

lines = """1:some text:othertext
2:sometext:othertext
3:sometext:othertext"""

lines = lines.split('\n')

for line in lines:
    key, some, other = line.split(':')
    print '%s  %s' % [line, other]

答案 3 :(得分:0)

这将根据需要生成输出

text = '1:sometext:othertext'

text = " ".join(text.split(":")[2:])

'1:sometext:othertext' - > othertext

'1:sometext:othertext:abc:jkl' - > othertext abc jkl

相关问题