How to remove some dots from a string in python

remove substring from string python
remove all occurrences of a character in a string python
remove fullstop from string python
remove character from string python
how to remove a word from a string in python
write a program to remove special characters and numbers in the input string in python
python remove all lowercase from string
remove escape characters from string python

I'm extracting an int from a table but amazingly comes as a string with multiple full stops. This is what I get:

p = '23.4565.90'

I would like to remove the last dots but retain the first one when converting to an in. If i do

print (p.replace('.',''))

all dot are removed How can I do this.

N/B Tried a long way of doing this

p = '88.909.90000.0'
pp = p.replace('.','')
ppp = list(''.join(pp))
ppp.insert(2, '.')
print (''.join(ppp))

BUT discovered that some figures come as e.g. 170.53609.45 and with this example, I'll end up with 17.05360945 instead of 170.5360945

What about str.partition?

p = '23.4565.90'
a, b, c = p.partition('.')
print(a + b + c.replace('.', ''))

This would print: 23.456590

EDIT: the method is partition not separate

Python Remove Character from String, String replace() function arguments is string. Let's see how to remove a word from a string. s = 'ab12abc34ba' print(s.replace(� Lets discuss certain ways to perform this particular task. Method #1 : Using replace () One can use replace () inside a loop to check for a bad_char and then replace it with the empty string hence removing it. This is the most basic approach and inefficient on performance point of view. filter_none.

Here is a solution:

p = '23.4565.90'

def rreplace(s, old, new, occurrence):
    li = s.rsplit(old, occurrence)
    return new.join(li)

# First arg is the string
# Second arg is what you want to replace
# Third is what you want to replace it with
# Fourth is how many of them you want to replace starting from the right.
#    which in our case is all but the first '.'
d = rreplace(p, '.', '', p.count('.') - 1) 
print(d)

>>> 23.456590

Credit to How to replace all occurences except the first one?.

How to remove specific characters from a string in Python?, The string class has a method replace that can be used to replace substrings in a string. We can use this method to replace characters we want� If you want to remove multiple characters from a string in a single line, it's better to use regular expressions. You can separate multiple characters by "|" and use the re.sub (chars_to_replace, string_to_replace_with, str).

This isnt the best way in doing it especially if your not getting similar values but if you do some sort of loop to check how many values there are then do this it should work. but if you only have three dots in the string try this:

p = '88.909.90000.0'
p = p.split('.')
p = p[0] + '.' + p[1] + p[2]

then if you want it as a number do this p = float(p)

Python, Lets discuss certain ways to perform this particular task. Method #1 : Using replace() One can use replace() inside a loop to check for a bad_char� 1 Python Remove Character from String. 1.1 Python Remove Character from String using replace () 1.2 Python Remove Character from String using translate () 2 Removing Spaces from a String. 3 Python Remove newline from String. 4 Remove substring from string. 5 Remove specified number of times.

Python String, strip() in-built function of Python is used to remove all the leading and trailing spaces from a string. Parameters : remove (optional): Character or a set of characters, that needs to be removed from the string. The function can take one or no parameter. Remove specific characters from a string in Python (27 answers) Closed 6 years ago . I'm a beginner with both Python and RegEx, and I would like to know how to make a string that takes symbols and replaces them with spaces.

How to remove punctuation from a Python string, What are some instances where end=" " can be used in Python? 571 Views � How do e.g. Best way to strip punctuation from a string in Python. 48.4K views �. Any characters in line which are in that class are replaced with the second parameter to sub: an empty string. In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for str.translate.

How to remove multiple characters from a string in Python, How to remove multiple characters from a string in Python. Use str.replace(). Use re.sub(). Removing multiple characters from a string results in a new string that� Assuming you want to remove the domain, no matter what it is (.com, .net, etc). I recommend finding the . and removing everything from that point on. url = 'abcdc.com' dot_index = url.rfind('.') url = url[:dot_index] Here I'm using rfind to solve the problem of urls like abcdc.com.net which should be reduced to the name abcdc.com.

Comments
  • I tested with examples with multiple . and with no .. It seems to work.
  • You could for loop this solution for any number of .
  • Yes. Im working on that know ill update my answer once i've done that