Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string manipulation
#1
I need to replace parts of a string with what it matches in a dictionary. I have restrictions and to change strings only functions i can use are .strip, .split, .append, .join, .format. I cant use replace.

def sub(my_dict, phrase):
    for keys, values in my_dict.items():
        s = phrase.split(keys)
        phrase = values.join(s)
    return phrase
my_dict = { 'c':'.', 'p':'f', 'a':'g', 'l':'e', 'e':'l', ' ':'o' }
phrase = "ape pea lap"
should return gfloflgoegf

I return gfloflgolgf
Reply


Messages In This Thread
string manipulation - by yogi123 - Apr-08-2018, 09:28 PM
RE: string manipulation - by scidam - Apr-09-2018, 01:29 AM
RE: string manipulation - by yogi123 - Apr-09-2018, 02:24 AM
RE: string manipulation - by scidam - Apr-09-2018, 10:58 PM
RE: string manipulation - by yogi123 - Apr-10-2018, 02:42 PM
RE: string manipulation - by scidam - Apr-12-2018, 04:08 AM

Forum Jump:

User Panel Messages

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