简体   繁体   中英

Python - Convert Underscore to Fullstop

Convert String '321_1' to '321.1' .

I would like to create a method to convert the underscore to full stop. i used split but it cant work .. Anyone can help me? or must i use while loop

Convert underscore to fullstop

def Convert_toFullStop(text):

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

使用replace()函数?

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

I would do

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

and leave the print to the caller.

The best is to use replace() method as it's suggested in the answers above.

But if you really want to use split() :

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

By default split() method splits by space character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM