Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I change a string.
#3
(Jun-03-2020, 10:13 AM)buran Wrote:
var = 'When parents goWhat do they listen toWhy'
words = ['When', 'Why', 'What', 'How']
for word in words:
    if not var.startswith(word):
        var = var.replace(word, f'. {word}')
print(var)

Thank you so much.
There is one 'but'. For example: if the string has When parents goWhat do they listen toWhy How
How to do not to set '.' in front of 'How' ?
output: When parents go. What do they listen to. Why How
Reply


Messages In This Thread
How can I change a string. - by Mike Ru - Jun-03-2020, 10:03 AM
RE: How can I change a string. - by buran - Jun-03-2020, 10:13 AM
RE: How can I change a string. - by Mike Ru - Jun-03-2020, 10:39 AM
RE: How can I change a string. - by buran - Jun-03-2020, 10:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  change string in MS word Mr_Blue 8 3,337 Sep-19-2021, 02:13 PM
Last Post: snippsat
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 2,538 May-24-2021, 08:48 AM
Last Post: bowlofred
  Change string into Dict Robin_at_Cantelli 2 2,936 Mar-05-2020, 10:37 AM
Last Post: vishalhule
  "replace() method" fails to change string pw928gts 4 3,420 Nov-30-2018, 05:48 PM
Last Post: nilamo
  Change values in string multiline DavidFernandez 4 3,098 Aug-26-2018, 08:09 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020