Try This

{unicodedata.normalize("NFKD", key): unicodedata.normalize("NFKD", item) for key, item in dict1.items()}

Output

 'Repeat interval': '23 days',
 'Epoch': '25 January 2015, 00:45:13 UTC[2]',
 'Band': 'S Band(TT&C support)X Band(science data acquisition)',
 'Bandwidth': 'up to 722kbit/s download (S Band)up to 18.4Mbit/s download (X Band)up to 4kbit /s upload (S Band)'}
Answer from WONDER on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › how to replace characters in a string in a dictionary?
r/learnpython on Reddit: How to replace characters in a string in a dictionary?
October 31, 2020 -

Hi! I was wondering if there's any way to replace characters in strings that are part of a dictionary? For example:

x = {"dog" : ["corgi", "Husky", "shiba"]} ----> x = {"Dog" : ["Corgi", "Husky", "Shiba"]}

In this example, I need to capitalize the first letter in each string if it's not capitalized already. I can't use any string methods and the only list method I can use is .insert(). I can use .chr() and .ord()

Discussions

python - String replacement using dictionaries - Code Review Stack Exchange
I've always been bothered by the fact that there weren't any built-in functions that could replace multiple substrings of a string in Python, so I created this function. Essentially, you supply it with a string, a dictionary of keys (substrings) and values (replacements), and then a few additional ... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
July 18, 2015
python - How to recursively replace character in keys of a nested dictionary? - Stack Overflow
I am guessing you have the same ... insert dictionaries that have keys with dots (.) in them. This solution is essentially the same as most other answers here, but it is slightly more compact, and perhaps less readable in that it uses a single statement and calls itself recursively. For Python 3. def replace_keys(my_dict): ... More on stackoverflow.com
🌐 stackoverflow.com
April 29, 2017
python - Replace character in dict keys recursively - Stack Overflow
I'm current working a script that takes an Android application's metadata as a nested dictionary and inserts it to MongoDB. However, since some of the keys include '.'s (due to component names in t... More on stackoverflow.com
🌐 stackoverflow.com
How can I replace all \n characters in a dictionary?
Use "\n" instead of "\\n" More on reddit.com
🌐 r/learnpython
8
1
July 1, 2021
Top answer
1 of 1
9

Mostly this looks good, but I’ll make a few suggestions:

Boolean arguments in your function signature

These control whether certain strings/mappings are lowercased first. I think this behaviour should be removed from keymap_replace(). It's scope creep: it’s gone from being string replacements to more complicated string manipulations. I’d leave that sort of manipulation to the caller.

(And you could continue to add options like this: what about uppercasing the string, or reversing it, or another pattern. Rather than muddling up your function with this, I’d just take it out.)

That makes the arguments simpler, and simplifies the function definition as well.

But if you really want those parameters in there, then I’d suggest making them keyword-only. In Python 3, you can insert an asterisk as so:

def keymap_replace(
    string: str, 
    mappings: dict,
    *,
    lower_keys=False,
    lower_values=False,
    lower_string=False,
) -> str:

and when the function is called, those boolean arguments have to be as keyword arguments, not positional. There can never be ambiguity in how the function is being used.

Dictionaries are unordered

There’s a possibility for ambiguity, or for substitutions to be chained. For example:

keymap_replace('Hello world', {
    'J': 'K',
    'H': 'J'
})

Should this call return "Jello world" or "Kello world"?

I think the first is more natural – I don’t expect substitutions to be chained – but when I tried the function, I got the latter. You should try to make this less confusing.

One possibility that springs to mind:

replaced_string = ''.join(mappings.get(char, char) for char in string)

Each character in the original string is subject to at most one substitution.

🌐
GeeksforGeeks
geeksforgeeks.org › python › replacing-characters-in-a-string-using-dictionary-in-python
Replacing Characters in a String Using Dictionary in Python - GeeksforGeeks
July 23, 2025 - Get method checks if the character has a replacement; otherwise, it appends the original character. If the dictionary keys contain multiple characters or patterns, re.sub() can be used for replacements.
🌐
IQCode
iqcode.com › code › python › replace-key-of-dictionary-python
replace key of dictionary python Code Example
October 28, 2021 - key dictionary python replace keys ... python change key in dict python3 alter keys in dict how to update names of keys in python python dictionary replace all values replace each character in a string based on dictionary python python .string.replace with dict replace the ...
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › how can i replace all \n characters in a dictionary?
r/learnpython on Reddit: How can I replace all \n characters in a dictionary?
July 1, 2021 -

Hello,

I'm not sure if this is possible, but I have some data in the form of a dict. The problem is that sometimes both keys and values contains the \n character.

For example:

foo = {
    "bar\n": "\n",
    "foo\n": "elo",
    "foobs": "\n",
    ... 
}

I would like to replace all of the \n characters with an empty string -> ""

I tried:

empty_dict = {}

for key, value in foo.items():
    x = key.replace("\\n", "")
    y = value.replace("\\n", "")
    empty_dict[x] = y

print(empty_dict)
----------------------------------
{'bar\n': '\n', 'foo\n': 'elo', 'foobs': '\n'}

expected:
{'bar': '', 'foo': 'elo', 'foobs': ''}

Is this possible to do?

Thank you!

🌐
Stack Overflow
stackoverflow.com › questions › 25485326 › python-replacing-characters-in-a-dictionary-of-strings-using-a-dictionary
Python replacing characters in a dictionary of strings using a dictionary - Stack Overflow
I need to loop through each character of each key in words and replace any character that corresponds to any key in the clues dictionary. ... Letting you know that you haven't been clear with the expected output part. ... That clues dictionary is the opposite of being helpful. It would be more useful if the symbol was the key, and the decrypted letter was the value. ... Any chance you could invert the role of keys and values in the clues dict? Otherwise lookups might be quite expensive. ... #!/usr/bin/python #-*- coding:utf-8 -*- clues = {'!':'A', '2':'B', '^':'C'} d = ['!2^', '^!2'] words = {} for i in d: words[i]='' for x in i: words[i] += clues[x] print words
🌐
UC Berkeley
inst.eecs.berkeley.edu › ~cs10 › labs › cur › programming › python › advanced_dictionary_problems.html
More Dictionary Problems
Can a dictionary have identical keys? Talk with your partner, then try using your function on one such dictionary, like this: invert_dict({"A":"X", "B":"Z", "C":"Z"}) and see if you were right. Good job! Now you can substitute characters in a string, then recover the original string like so: ...
Top answer
1 of 2
22

You can write a recursive function, like this

from collections.abc import Mapping
def rec_key_replace(obj):
    if isinstance(obj, Mapping):
        return {key.replace('.', '_'): rec_key_replace(val) for key, val in obj.items()}
    return obj

and when you invoke this with the dictionary you have shown in the question, you will get a new dictionary, with the dots in keys replaced with _s

{'delicious_apples': {'green_apples': 2}, 'green_pear': 4, 'brown_muffins': 5}

Explanation

Here, we just check if the current object is an instance of dict and if it is, then we iterate the dictionary, replace the key and call the function recursively. If it is actually not a dictionary, then return it as it is.

2 of 2
7

Assuming . is only present in keys and all the dictionary's contents are primitive literals, the really cheap way would be to use str() or repr(), do the replacement, then ast.literal_eval() to get it back:

d ={
    "brown.muffins": 5,
    "green.pear": 4,
    "delicious_apples": {
        "green.apples": 2
    } # correct brace
}

Result:

>>> import ast
>>> ast.literal_eval(repr(d).replace('.','_'))
{'delicious_apples': {'green_apples': 2}, 'green_pear': 4, 'brown_muffins': 5}

If the dictionary has . outside of keys, we can replace more carefully by using a regular expression to look for strings like 'ke.y': and replace only those bits:

>>> import re
>>> ast.literal_eval(re.sub(r"'(.*?)':", lambda x: x.group(0).replace('.','_'), repr(d)))
{'delicious_apples': {'green_apples': 2}, 'green_pear': 4, 'brown_muffins': 5}

If your dictionary is very complex, with '.' in values and dictionary-like strings and so on, use a real recursive approach. Like I said at the start, though, this is the cheap way.

🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Replacing a specific value in a dictionary - Curriculum Help - The freeCodeCamp Forum
February 26, 2019 - Hi, Below a part of the program I am writing, I have difficulty to make the last part function correctly. mwodict is a dictionary where the keys are the letters of a word chosen randomly with choice method, and the values are * The user should type a letter to try guessing the hidden word. l is the input() letter, in case the letter typed is one of the hidden word letters, the program should return the values ‘x’ of the word except the value of l which should be replacing it’s corresponding...
🌐
Bobby Hadz
bobbyhadz.com › blog › python-replace-words-in-string-using-dictionary
Replace words in a String using a Dictionary in Python | bobbyhadz
Strings are immutable in Python. Use the str.lower() method if you need to convert the dictionary's keys and values to lowercase. ... Copied!my_str = 'site | name' my_dict = { 'SITE': 'BOBBYHADZ.COM', 'NAME': 'BORISLAV' } for key, value in ...
🌐
Reddit
reddit.com › r/learnpython › q: how can i use a dictionary to replace characters in a string?
Q: How can I use a dictionary to replace characters in a string? : r/learnpython
October 7, 2016 - You can also handle the exception (KeyError) produced when the looked-up key is invalid. In the cases where you want to handle this by substituting some "default" value, you can instead use dictionary.get(letter, default). ... I also think this is probably possible to do without dictionaries, but I thought it would be fitting to use a dictionary when making a translator. How do you actually turn Python skills into freelance income?
🌐
Stack Overflow
stackoverflow.com › questions › 60104941 › replace-character-in-a-python-dictionary
Replace Character in a Python Dictionary - Stack Overflow
I have python dictionary like this and sometimes I get an mdash or some other character (like "\u2014") that I need to replace with a simply hyphen. I've seen lots of ways to change keys and whole