Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to remove commas between numbers in a Python string

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report