Get the App
SLTechnology News&Howtos  ›  Development  › 

How to remove commas between numbers in a Python string

Shulou Source: shulou.com Published: 2022-05-31 16:46:41 09月26日 Update

This article mainly explains how to remove the comma between numbers in the Python string. The explanation in the article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn how to remove the comma between numbers in the Python string.

The string removes the comma between numbers

In the representation of western numbers, many formats are similar to this: 123456789.

If you get such a string and convert it directly to an integer using int, you will definitely report an error, then you need to remove the comma between the numbers before the format conversion.

If the string has only numbers and ",", then you can replace it with a replace.

For example:

> n = '123456789' > > N1 = n.replace (',',') > print n1123456789

However, replace substitution is a bit domineering when the string includes numbers and other characters. For example: Today is Sunday, I bought $100000. Replacing it directly with replace removes comma punctuation from the string.

So you need to find numbers, numbers, commas before this format.

The code looks like this:

Import res = 'Today is Sunday, I bought $100000.roomp = re.compile (r'\ d,\ d') while 1: M = p.search (s) if m: mm = m.group () s = s.replace (mm,mm.replace (',')) else: breakprint s

Judge by regular expressions.

Delete a symbol from a string

Deletes the specified symbol in a string

S = "abc123123." # Delete the comma s = s.replace (',',') print (s) # "abc123123."

Delete spaces in a string

S = "123abc" # delete the beginning of the space print (s.lstrip ()) # "123abc" # delete the end of the space print (s.rstrip ()) # "123abc" # delete the beginning and end of the space print (s.strip ()) # "123abc" # delete all spaces in the string print (s.replace (',')) # "123abc"

Delete all symbols in the string, leaving only numbers and letters

Import res = "123 dddA ABC.? / &?" ^ _ ^. " # replace all characters encoded with non -\ u0030 -\ u0039 (numbers),\ u0041 -\ u007a (English letters) with the empty string rs = re.sub ("([^\ u0030 -\ u0039\ u0041 -\ u007a])",', s) print (rs) # "123abcdddA"

Only reserved Chinese characters in the string

Import res = "I love China? I love China." # replace all characters encoded with non -\ u4e00 -\ u9fa5 (Chinese characters) with the empty string rs = re.sub ("([^\ u4e00 -\ u9fa5])",'', s) print (rs) # "I love China"

Corresponding unicode coding range

Explain the unicode range numbers\ u0030 -\ u0039 Chinese characters\ u4e00 -\ u9fa5 uppercase letters\ u0041 -\ u005a lowercase letters\ u0041 -\ u007a Korean\ uAC00-\ uD7AF Japanese\ u3040 -\ u31FF thank you for reading, the above is the content of "how to remove commas between numbers in Python string", after the study of this article I believe you have a deeper understanding of how to remove the comma between numbers in the Python string, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Characters strings numbers commas between letters spaces symbols formats coding Chinese characters English learning content beginning I love empty characters range Chinese overbearing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MySQL Huawei Shulou Tech Info NVidia