Wednesday, February 20, 2019

Dictionary Update values

d = {1: "one", 2: "three"}
d1 = {2: "two"}

# updates the value of key 2
d.update(d1)
print(d)

d1 = {3: "three"}

# adds element with key 3
d.update(d1)
print(d)

O/P:


Removing Punctuation's from a string

Problem
You have a text and you want to remove punctuations from it. Example:
in:
"Hello! It is time to remove punctuations here. It is easy, you will see."

out:
"Hello It is time to remove punctuations here It is easy you will see"
Solution
Let’s see a Python 3 solution:
1
2
3
4
5
>>> import string
>>> tr = str.maketrans("", "", string.punctuation)
>>> s = "Hello! It is time to remove punctuations here. It is easy, you will see."
>>> s.translate(tr)
'Hello Its time to remove punctuations here Its easy youll see'