2013-10-02 65 views
1

将字符串'321_1'转换为'321.1'Python - 将下划线转换为Fullstop

我想创建一个方法将下划线转换为句号。我用拆分,但它不能工作..任何人都可以帮助我?或者我必须使用while循环

转换下划线句号

def Convert_toFullStop(text): 

    x1 = "" 
    words = text.split() 
    if words in ("_"): 
     words = "." 
    print words 

回答

3

我会做

def Convert_toFullStop(text): 
    return text.replace('_', '.') 

,离开print给调用者。

7

使用replace()功能?

newtext = text.replace('_','.') 
+1

python就像口语。下次尝试它之前,请问:-) – HongboZhu

+1

@ user2837162不是一个函数,它是一个字符串对象的方法。 –

+0

好吧谢谢:)它运作良好 – user2837162

1

最好的是使用replace()方法,因为它在上面的答案中提出。

但是,如果你真的想用split()

words = text.split("_") 
print ".".join(words) 

通过用空格字符默认split()方法拆分。

相关问题