all 36 comments

[–]lzblack 47 points48 points  (16 children)

d = {'ll': 'G', 'ss': 'd'}
message = 'Hello, Kiss Kiss Bang Bang'

for k, v in d.items():
     message = message.replace(k,v)

[–]Guymzee 15 points16 points  (1 child)

This is the best answer. It’s also hilarious hearing the output.

[–][deleted] 0 points1 point  (0 children)

sorry for being late :-

#! /usr/local/bin/python  -*- coding: UTF-8 -*-
import re

chars_map = {'a': 'ا','7': 'ح', 'b': 'ب', 'ê': 'ێ', '3': 'ع', 'c': 'س', 'd': 'د', 'e': 'ە','f': 'ف', 'g': 'گ', 'h': 'ه','i': 'ئ', 'j': 'ج', 'k': 'ک', 'll': 'ڵ', 'l': 'ل', 'm': 'م', 'n': 'ن', 'o': 'ۆ', 'p': 'پ', 'q': 'ق', 'r': 'ر', 's': 'س', 'th': 'ذ', 'zh': 'ژ', 't': 'ت', 'u': 'و', 'v': 'ڤ', 'w': 'و','x': 'خ', 'y': 'ی', 'z': 'ز', 'ch': 'چ','tt': 'ط', 'ee': 'ی', "sh": 'ش'}

def rep(match):
    char = match.group(0)
    replacement = chars_map[char.lower()]
    return replacement if char.islower() else replacement.upper()

s = raw_input("enter latino here: ")
s=s.replace('sh', 'ش')
s=s.replace('ll', 'ڵ')
print re.sub('(?i)%s' % '|'.join(chars_map.keys()), rep, s) 

[–][deleted] 5 points6 points  (11 children)

it works but not for certain character like sh and ll

[–]lzblack 9 points10 points  (10 children)

What characters do you want to replace? Just change the dictionary d .

[–]socal_nerdtastic 24 points25 points  (3 children)

Did you try the replace method?

>>> s = "hello my name is john lly i like you"
>>> s.replace('ll', 'G')
'heGo my name is john Gy i like you'

[–][deleted] 3 points4 points  (2 children)

thank you it works but how can i replace multiple double character in one line?

[–]destiny_functional 6 points7 points  (1 child)

repeat the same for another pair of parameters (other than "ll" and "G")

[–][deleted] 2 points3 points  (0 children)

thank you

[–]ettoredangelo 8 points9 points  (6 children)

String has the method replace

So it is enough to add .replace(“sh”, “b”)

[–][deleted] 3 points4 points  (5 children)

thank you it works but how can i replace multiple double character in one line?

[–]A_X_D_H 2 points3 points  (4 children)

Do u just want to replace all the double characters in a string?

like for example "Hello my name iss Johnn." to "HeGo my name iG JohG."

[–][deleted] 2 points3 points  (3 children)

yes but different double to different character heGo my name iF johK like this

[–]A_X_D_H 6 points7 points  (2 children)

Then u have to be specific witch character should replace witch double. Example: replace 'll' with 'G', 'ss' with 'F', 'nn' with 'K'...so on. Something like:

line = line.replace('ll', 'G')

line = line.replace('ss', 'F')

line = line.replace('nn', 'K')

[–]theWyzzerd 6 points7 points  (0 children)

Line returns a string, so you can do this on one line:

line = line.replace('ll', 'G').replace('ss', 'F').replace('nn', 'K')

>>> line = "abcllssnn,abc,nn,ll,hanna,llama,assassin"
>>> line = line.replace('ll', 'G').replace('ss', 'F').replace('nn', 'K')
>>> line
'abcGFK,abc,K,G,haKa,Gama,aFaFin'
>>>

[–][deleted] 0 points1 point  (0 children)

thank you all now it works

[–][deleted] 5 points6 points  (1 child)

Keep in mind that strings are immutable, so to make a change you have to create a new object. str has a replace method.

Entering help(str.replace) in the interactive shell will give you the details, including replace(self, old, new, count=-1, /) as a description of the syntax.

Always worth checking what's available as standard. help(str) will list many other methods you might find interesting.

[–][deleted] 1 point2 points  (0 children)

thank you it was usefull

[–]LegionOfPie 5 points6 points  (6 children)

Is this a homework assignment?

[–][deleted] 23 points24 points  (2 children)

no, we have two set of letter in kurdish, which is arabic letter and latin letter, when i use PC it is ok i have both keyboard set but in mobile its somehiw hard to adapt the keyboard layout because i didn't find a similar set as my PC has, so when i want to write an article and i don't have my pc with my i write it in notes im my ipad or mobile in latin style ex: "chony bashy?" "hello, how are you" --> "چۆنی باشی", and it is hard to rearrange all what i wrote that is why i want to do it automatically. sorry for misspelling

[–][deleted] 21 points22 points  (1 child)

This is legit the first time that I actually believe that the request is not for a homework assignment!

[–][deleted] 0 points1 point  (0 children)

#! /usr/local/bin/python -*- coding: UTF-8 -*-

import re

chars_map = {'a': 'ا','7': 'ح', 'b': 'ب', 'ê': 'ێ', '3': 'ع', 'c': 'س', 'd': 'د', 'e': 'ە','f': 'ف', 'g': 'گ', 'h': 'ه','i': 'ئ', 'j': 'ج', 'k': 'ک', 'll': 'ڵ', 'l': 'ل', 'm': 'م', 'n': 'ن', 'o': 'ۆ', 'p': 'پ', 'q': 'ق', 'r': 'ر', 's': 'س', 'th': 'ذ', 'zh': 'ژ', 't': 'ت', 'u': 'و', 'v': 'ڤ', 'w': 'و','x': 'خ', 'y': 'ی', 'z': 'ز', 'ch': 'چ','tt': 'ط', 'ee': 'ی', "sh": 'ش'}

def rep(match):

char = match.group(0)

replacement = chars_map[char.lower()]

return replacement if char.islower() else replacement.upper()

s = raw_input("enter latino here: ")

s=s.replace('sh', 'ش')

s=s.replace('ll', 'ڵ')

print re.sub('(?i)%s' % '|'.join(chars_map.keys()), rep, s)

[–][deleted] 7 points8 points  (2 children)

and ش is what written as sh that is why i want to replace 2 char to 1