Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I change a string.
#1
I have a string. I need to find words and change them if it there are over there.
I am trying something like this. But I doesn't work.
I need to get new string something like this.
When parents go. What do they listen to. Why
var = 'When parents goWhat do they listen toWhy'
words = ['When', 'Why', 'What', 'How']
def check(a):
    s = []
    for x in a:
        for j in words:
            if j in x:
                s.append(foo(x))
    return s

def foo(a):
    first = list(a)[0]
    var = list(a)[1:]
    new = []
    for x in var:
        if x.isupper():
            new.append('. ')
            new.append(x)
        else:
            new.append(x)
    new_string = first + ''.join(new)
    return new_string

var = var.split(' ')
print(check(var))
How can I do this correctly ?
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 814 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 960 May-24-2021, 08:48 AM
Last Post: bowlofred
  Change string into Dict Robin_at_Cantelli 2 1,910 Mar-05-2020, 10:37 AM
Last Post: vishalhule
  "replace() method" fails to change string pw928gts 4 2,046 Nov-30-2018, 05:48 PM
Last Post: nilamo
  Change values in string multiline DavidFernandez 4 1,776 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