Remove duplicate letters in a string? [duplicate] - python

This question already has answers here:
Given a string how can I remove all the duplicated consecutive letters?
(5 answers)
Closed 1 year ago.
How can i make a basic function with def in python that removes duplicate letters from a string?
Example: input: "abbcdddea"; output: "abcdea"

This code removes duplicates but conserves the order:
string = "abb"
string_without_duplicates = "".join(dict.fromkeys(string))

Related

Replace a specific character from string [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Changing one character in a string
(15 answers)
Closed 1 year ago.
I know how to replace characters, but I would like to have the instance done once. My code replace's all the characters before.
string = "Forever9999"
string = string[:-4] + string[-4:].replace("9", "1")
Which in the end would be Forever1999, but I get Forever1111
Any help appreciated.
You can pass another paramter to str.replace(old, new[, count]), which is the max count of occurrences you want to replace:
string.replace("9", "1", 1)
# 'Forever1999'

How to return the last character of a string in Python? [duplicate]

This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
Remove final character from string
(5 answers)
Closed 1 year ago.
How can I get the last character of this string?
seed_name = "Cocoa"
As shown in the official Python tutorial,
>>> word = 'Python'
[...]
Indices may also be negative numbers, to start counting from the right:
>>> word[-1] # last character
'n'

String index. How to retrieve last 3 letters of strings [duplicate]

This question already has answers here:
Get the last 4 characters of a string [duplicate]
(2 answers)
Closed 1 year ago.
streetName = "Finley"
print(streetName[???]),
^
|
I'm trying to find out how I can retrieve the last 3 letters of the string using string index. What would I put in the brackets??
print(streetName[-3:]).
This should work.

How to delete certain words from string without spaces? [duplicate]

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')

How to make a word of all letters in a list? (Python) [duplicate]

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"])

Categories