简体   繁体   中英

Figuring out how to remove pieces of a string

I need to replace the spaces in a given sentence without replacing the spaces in the first two words, is there a way to do that in python?

If you post an example it would be easier to answer but here's a guess at what you're asking for

# Slicing method
s = "Word one Word two rest of sentence"
s = s[:17] + s[17:].replace(' ', '_')
# result: 'Word one Word two_rest_of_sentence'

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