简体   繁体   中英

Remove spaces from user input string in Python

how can I remove whitespaces from within a user input string? Can't I use .replace(" ","") ? it didn't work for me.
My code:

>>>p=raw_input ("Enter phrase:\n")    
>>>p.replace(" "."")

It still outputs user input phrase WITH spaces..what am I doing wrong?Please help.

由于字符串在 python 中是不可变的,因此str.replace返回一个新字符串,它不会修改现有字符串

p = p.replace(" ","")

You can use replace, but you have to assign its return to the variable

>>>p=raw_input ("Enter phrase:\n")

>>>p=p.replace(" ","")

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