Python将下划线转换为句号

2024-10-03 00:23:12 发布

您现在位置:Python中文网/ 问答频道 /正文

将字符串'321_1'转换为'321.1'。你知道吗

我想创建一个方法来将下划线转换为句号。我用分裂法,但它不能工作。。有人能帮我吗?还是必须使用while循环

将下划线转换为句号

def Convert_toFullStop(text):

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

Tags: 方法字符串textinconvertifdefsplit
3条回答

最好的方法是使用上面答案中建议的replace()方法。你知道吗

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

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

默认情况下,split()方法按空格字符分割。你知道吗

使用replace()函数?你知道吗

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

我会的

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

print留给调用者。你知道吗

相关问题 更多 >