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
#2
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#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
#4
well this breaks the example both in terms of example input and expected output.
It breaks the example input as I understood it - a word in words list is concatenated to previous word, i.e. my understandig was the input would be When parents goWhat do they listen toWhyHow
It breaks the expected output as I understood it - a word in list is preceded by dot and space, i.e. making a complete sentence.
When parents go. What do they listen to. Why. How

All that said - define explicit rules how the input string will look like and what the expected output should be and then a more complex parser should be created
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  change string in MS word Mr_Blue 8 559 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 782 May-24-2021, 08:48 AM
Last Post: bowlofred
  Change string into Dict Robin_at_Cantelli 2 1,811 Mar-05-2020, 10:37 AM
Last Post: vishalhule
  "replace() method" fails to change string pw928gts 4 1,912 Nov-30-2018, 05:48 PM
Last Post: nilamo
  Change values in string multiline DavidFernandez 4 1,647 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