How do i remove a dot from a string in 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

asked Apr 18, 2019 at 0:21

How do i remove a dot from a string in python?

Here is a solution:

p = '23.4565.90'

def rreplace(string: str, find: str, replace: str, n_occurences: int) -> str:
    """
    Given a `string`, `find` and `replace` the first `n_occurences`
    found from the right of the string.
    """
    temp = string.rsplit(find, n_occurences)
    return replace.join(temp)

d = rreplace(string=p, find='.', replace='', n_occurences=p.count('.') - 1)

print(d)

>>> 23.456590

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

answered Apr 18, 2019 at 0:34

How do i remove a dot from a string in python?

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

answered Apr 18, 2019 at 0:41

1

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)

answered Apr 18, 2019 at 0:48

DextronDextron

5986 silver badges23 bronze badges

2

def add_dots(to_add):
    res=""
    for letter in to_add:
        res= res + letter + "."
        print{:-1}res)
    
def remove_dots(to_remove): 
    print (to_remove):
    
add_dots("test")    
remove_dots("t.e.s.t.")

How do i remove a dot from a string in python?

RiveN

2,50311 gold badges10 silver badges24 bronze badges

answered May 31 at 13:12

1

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:,Making statements based on opinion; back them up with references or personal experience.,I'm extracting an int from a table but amazingly comes as a string with multiple full stops. This is what I get:,I would like to remove the last dots but retain the first one when converting to an in. If i do

Here is a solution:

p = '23.4565.90'

def rreplace(string: str, find: str, replace: str, n_occurences: int) - > str:
   ""
"
Given a `string`, `find`
and `replace`
the first `n_occurences`
found from the right of the string.
""
"
temp = string.rsplit(find, n_occurences)
return replace.join(temp)

d = rreplace(string = p, find = '.', replace = '', n_occurences = p.count('.') - 1)

print(d)

   >>>
   23.456590

What about str.partition?

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

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]

def add_dots(to_add):
   res = ""
for letter in to_add:
   res = res + letter + "."
print {
   : -1
}
res)

def remove_dots(to_remove):
   print(to_remove):

   add_dots("test")
remove_dots("t.e.s.t.")


Suggestion : 2

Sometimes we want to remove all occurrences of a character from a string. There are two common ways to achieve this.,i want to replace lowercase characters before and after key in given string “there is A key to Success”,String replace() function arguments is string. Let’s see how to remove a word from a string.,Home » Python » Python Remove Character from String

Note that the string is immutable in Python, so this function will return a new string and the original string will remain unchanged.

s = 'abc12321cba'

print(s.replace('a', ''))

Python string translate() function replace each character in the string using the given translation table. We have to specify the Unicode code point for the character and ‘None’ as a replacement to remove it from the result string. We can use ord() function to get the Unicode code point of a character.

s = 'abc12321cba'

print(s.translate({
   ord('a'): None
}))

If you want to replace multiple characters, that can be done easily using an iterator. Let’s see how to remove characters ‘a’, ‘b’ and ‘c’ from a string.

s = 'abc12321cba'

print(s.translate({
   ord(i): None
   for i in 'abc'
}))


Suggestion : 3

The strip() method in-built function of Python is used to remove all the leading and trailing spaces from a string.,Returns: A copy of the string with both leading and trailing characters stripped.,In case the character of the string to the left doesn’t match with the characters in the char parameter, the method stops removing the leading characters.,In case the character of the string to the right doesn’t match with the characters in the char parameter, the method stops removing the trailing characters.

Output : 

Geeks
for Geeks
for
Geeks
for Geeks

Output: 

Geeks
for Geeks
Geeksforgeeks

Output:

Length before strip()
17
Length after removing spaces
15


Suggestion : 4

#
if you have arrays of number
a = [10.000, 20.000, 25.000]
b = [int(i) for i in a]
print(b) #[10, 20, 25]
#
if you have arrays of number which are string
a = ["10.000", "20.000", "25.000"]
b = [i.replace(".", "") for i in a]
print(b) #['10000', '20000', '25000']


Suggestion : 5

Javascript remove all dots from a string using replace(),Javascript remove all dots from a string using replaceAll(),Javascript remove all dots from a string using loop,Javascript remove all dots from a string using split() and join()

Code:-

let dummyString = "This.Is.Javascript."
dummyString = dummyString.replace(/\./g, '');
console.log(dummyString)

Function Code:-

function removeDots(_string) {
   let dummyString = String(_string);
   let finalString = ''
   for (let i = 0; i < dummyString.length; i++) {
      if (!(dummyString.charAt(i) === ".")) {
         finalString = finalString + dummyString.charAt(i);
      }
   }
   return finalString;
}


Suggestion : 6

How to replace string using JavaScript RegExp?,How to detect and replace all the array elements in a string using RegExp in JavaScript? ,Replacing dots with dashes in a string using JavaScript,How to replace Digits into String using Java?

You can try to run the following code to replace all dots in a string −

<!DOCTYPE html>
<html>
   

<head>
      </head>
   

<body>
         <script>
      var str = 'Demo.Text.with.dots';
      document.write("Text with dots- " + str);

      var res = str.replace(/\./g, ' ');
      document.write("<br>Text without dots- " + res);
   </script>
      </body>

</html>


Suggestion : 7

Call the replaceAll method, passing it a dot as the first parameter and an empty string as the second - str.replaceAll('.', '').,To remove all dots from a string:,Call the replace method, passing it a regular expression that matches all dots as the first parameter and an empty string as the second.,Remove all Commas from a String in JavaScript

Copied!
   const str = 'a.b.c.';

const dotsRemoved = str.replaceAll('.', '');
console.log(dotsRemoved); 

Copied!
   const str = 'a.b.c.';

const dotsRemoved = str.replace(/\./g, '');
console.log(dotsRemoved); 


How do I remove a dot from a string?

Use the String. replace() method to remove all dots from a string, e.g. const dotsRemoved = str. replace(/\./g, ''); . The replace() method will remove all dots from the string by replacing them with empty strings.

How do you remove punctuation from a string in Python?

One of the easiest ways to remove punctuation from a string in Python is to use the str. translate() method. The translate() method typically takes a translation table, which we'll do using the . maketrans() method.

How do I remove special characters from a string in Python?

Using 're..
“[^A-Za-z0–9]” → It'll match all of the characters except the alphabets and the numbers. ... .
All of the characters matched will be replaced with an empty string..
All of the characters except the alphabets and numbers are removed..

How do I remove a dot from a Dataframe in Python?

First of all, create a data frame with a column having dot at last position in every value. Then, use gsub function to remove the dot at last position from every value in the column.