How to remove duplicate string in string in python [duplicate] - python

This question already has answers here:
How can I tell if a string repeats itself in Python?
(13 answers)
Closed 1 year ago.
I want to keep a single sentence in string with many the same sentences.
Input:
str1 = 'I want to do this to help her to get the job done I want to do this to help her to get the job done'
Expected output:
str2 = 'I want to do this to help her to get the job done'
Not missing some "to". Thanks.

out = []
for word in str1.split():
if word not in out:
out.append(word)
res = " ".join(out)

Related

How can I print the first letter of each word of a string? [duplicate]

This question already has answers here:
Extract first character of each word from the sentence string [closed]
(3 answers)
Closed 1 year ago.
I am new to Python and I want to experiment more with string manipulation.
I would like to print the first letter of each word of the statement...
'I like sweet and savoury food'
... so that the output looks like this:
'I l s a s f'
A simple "pythonic" way to do this is
print(' '.join(word[0] for word in sentence.split()))
Having a for loop like this is called a list comprehension. You can write it out in full detail:
split = sentence.split()
result = ''
for word in split:
result += word[0] + ' '
print(result.strip()) # strip to get rid of trailing space

How to get a string after and before a specific substring in Python? [duplicate]

This question already has answers here:
Split a string by a delimiter in python
(5 answers)
Closed 2 years ago.
How can I get a string after and before a specific substring?
For example, I want to get the strings before and after : in
my_string="str1:str2"
(which in this case it is: str1 and str2).
Depending on your use case you may want different things, but this might work best for you:
lst = my_string.split(":")
Then, lst will be: ['str1', 'str2']
You can also find the index of the substring by doing something like:
substring = ":"
index = my_string.find(":")
Then split the string on that index:
first_string = my_string[:index]
second_string = my_string[index+len(substring):]

How do I move the second word BEFORE the first? [duplicate]

This question already has answers here:
Reverse the ordering of words in a string
(48 answers)
Closed 4 years ago.
If my variable is:
string = "first second"
How can I change that to print:
second first
annoyingly simple but I can't find a solution for it!
I'd split the string on ' ' and then join it back:
s = 'first second'
tmp = s.split(' ')
result = ' '.join((tmp[1], tmp[0]))

How to change a split str into a normal str [duplicate]

This question already has an answer here:
How can I join a list of string into one string in Python
(1 answer)
Closed 5 years ago.
I have a code that does
a = "hello world"
a.split(' ')
then it does other things and after that when it prints it prints like this
['hello','world']
how would i make it change back to printing
hello word
Thanks for the help in advance!
Given a list, you can use ' '.join:
s = ['hello','world']
new_s = ' '.join(s)
Output:
'hello world'

Replace characters in a dict - Python 3 [duplicate]

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)

Categories