Can Python have support for number separators? - Python Help - Discussions on Python.org
Can Python have support for number separators? Python Help Daveson217 (David Babalola) February 23, 2024, 8:15pm 1 I see that when I call something like int(2,345,565), it gives an error. Interestingly, in Java, this is supported. Is it a good idea to have something like this in Python? Jelle (Jelle Zijlstra) February 23, 2024, 8:17pm 2 You’re a few years too late: % python Python 3.11.1 (main, Dec 21 2022, 16:19:04) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 2_345_678 2345678 >>> Added in Python 3.6 by PEP 515: Daveson217 (David Babalola) February 23, 2024, 8:58pm 3 Ouch .
I never knew this. I was revisiting Java and the thought crossed my mind. It’s good that I am now getting ideas to contribute to open source though. Lol Thanks a bunch Stefan2 (Stefan Pochmann) February 23, 2024, 9:37pm 4 How does the Java code look like? I was curious and googled it but can’t find it. kknechtel (Karl Knechtel) February 23, 2024, 10:47pm 5 I can’t find any evidence that Java supports any kind of thousands separator in the literal syntax for integers.
Of course it’s more than capable of parsing a string which contains thousands separators to create an integer (or floating-point) value. onePythonUser (Paul) February 24, 2024, 2:20am 6 Excuse me for chiming in. But since @Daveson217 specifically asked for a more natural number conversion to int using thousands comma separators (in the west and period for European based system), is there a number conversion built-in to int not using underscores whose syntax is not generally natural ? *** Update *** I suppose you can do your own conversion manually assuming you enter the number as a string, however: from re import sub def str_to_int(num): remv_comma = sub(',', '', num) return int(remv_comma) # test function add_nums = 123123 + str_to_int('4,123,123') print(add_nums) Daveson217 (David Babalola) February 24, 2024, 6:24am 7 You are right @kknechtel .
However, the code I speak of is in the Java source code for processing input, Scanner ; Python’s equivalent of the input function. See below: import java.util.Scanner; public class AskAge { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter your age"); int age = sc.nextInt(); } } Then somewhere down the rabbit hole of calling various methods (starting from the sc.nextInt(), the following method is called: private String processIntegerToken(String token) { String result = token.replaceAll(""+groupSeparator, ""); boolean isNegative = false; int preLen = negativePrefix.length(); if ((preLen > 0) && result.startsWith(negativePrefix)) { isNegative = true; result = result.substring(preLen); } int sufLen = negativeSuffix.length(); if ((sufLen > 0) && result.endsWith(negativeSuffix)) { isNegative = true; result = result.substring(result.length() - sufLen, result.length()); } if (isNegative) result = "-" + result; return result; } From the docs, it says: The integer token must be stripped of prefixes, group separators, and suffixes, non ascii digits must be converted into ascii digits before parse will accept it. cc: @Stefan2 Daveson217 (David Babalola) February 24, 2024, 6:26am 8 This works perfectly as well. @onePythonUser kknechtel (Karl Knechtel) February 24, 2024, 6:46am 9 Not really. input in Python gets a string, no matter what is typed.
It’s the equivalent of the readline method on a DataInputStream . You need to use other code to interpret the contents of the string to get other values of other types. java.util.Scanner is a completely different interface. You can make something work like that in Python, but it isn’t built-in and isn’t the normal way to do things in Python. Especially not when reading input from the user - because of how the terminal works , it will naturally be much simpler and less error-prone to read the input a line at a time consistently and do separate parsing on each line.
If you want to parse a string that is expected to represent a number that uses a thousands separator, see: TeamSpen210 (Spencer Brown) February 24, 2024, 7:03am 10 In addition to the locale module, you may want to look into the babel package, which provides more advanced facilities for handling specific languages and cultures. Daveson217 (David Babalola) February 24, 2024, 7:23am 11 Thanks for letting me know @kknechtel .
I have not used the locale package before.