This question already has answers here:
How to replace multiple substrings of a string?
(28 answers)
Closed 5 years ago.
I am working with Python 3 and I want to replace the emoticons included in a dictionary.
For example
text = "Hi, I'm coming home :)"
#Create dictionary
dict_lookup = {':(' : 'sad',
':)' : 'happy'}
The desired output is:
Hi, I'm coming home happy
What is the most efficient way to achieve this result in Python 3?
This should do the trick:
for emote, replacement in dict_lookup.items():
text = text.replace(emote, replacement)
Take a look at str.replace
It allows you to do text.replace(dict_key, dict_value)
Related
This question already has answers here:
How to remove substring from string in Python 3
(2 answers)
Closed 2 years ago.
Is there I way to delete words from a string in Python if it doesn't have spaces. For example, if you have the string "WUBHELLOWUB" I want to remove "WUB". I tried
s = 'WUBHELLOWUB'
while 'WUB' in s:
ind = s.find('WUB')
s = s[:ind] + s[ind+1:]
print(s)
but it did not work.
You can use regex
import re
data=r"\S*WUB\S*"
re.sub(data, '','WUBWUBHELLO')
This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 3 years ago.
How can I make a word from all the letters in a string?
For example:
(this is my list)
["h","e","l","l","o"]
And I want this as output:
hello
Try this :
"".join(["h","e","l","l","o"])
Use the join function which concatenate all the characters/substrings present in the list & return a single string.
name = ["h","e","l","l","o"]
concat_name = "".join(name)
print(concat_name)
Output :
hello
Try this :
''.join(["h","e","l","l","o"])
This question already has answers here:
How do I convert a list into a string with spaces in Python?
(6 answers)
Closed 4 years ago.
say you have a list like so:
lst = ['my', 'name', 'is', 'jack.']
If I convert to a string doing this:
''.join(lst)
output
'mynameisjack.'
How do I make the list print out:
"my name is jack."
instead of all together.
Instead of using ''.join(lst)(an empty string), use ' '.join(lst), with a space (see the documentation of join!).
This question already has answers here:
Split a string at uppercase letters
(22 answers)
Closed 6 years ago.
In general I have a string say
temp = "ProgramFields"
Now I want to split strings like these into two terms(I can identify tow strings based on uppercase character)
term1 = "Program"
term2 = "Field"
How to achieve this in python?
I tried regular expression and splitting terms but nothing gave me the result that I expected
Python code -
re.split("[A-Z][a-z]*","ProgramField")
Any suggestions?
You have to include groups:
re.split('([A-Z][a-z]*)', 'ProgramField)
This question already has answers here:
Extracting only characters from a string in Python
(7 answers)
How do you filter a string to only contain letters?
(6 answers)
Closed 6 years ago.
I was trying to figure out how to list just the letters in a string and ignore the numbers or any other characters. I figured out how to do it using the for loop, but I couldn't find out how to do it without using the for loop.
This is how I used the for loop:
>>> a = "Today is April 1, 2016"
for i in a:
if i.isalpha():
list(i)
Any help will be appreciated!
You can use filter for this:
>>> ''.join(filter(str.isalpha, a))
'TodayisApril'
list(set([x for x in a if x.isalpha()]))
this should do it :)