- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find number with thousand separator in Python
Suppose we have a number n, we have to return this number into string format where thousands are separated by comma (",").
So, if the input is like n = 512462687, then the output will be "512,462,687"
To solve this, we will follow these steps −
res := n as string
res := reversed form of res
ans := a blank string
for i in range 0 to size of res - 1, do
if i mod 3 is same as 0 and i is not same as 0, then
ans := ans concatenate ','
ans := ans concatenate res[i]
ans := reversed form of ans
Let us see the following implementation to get better understanding −
def solve(n): res = str(n) res = res[::-1] ans = "" for i in range(len(res)): if i%3 == 0 and i != 0 : ans += ',' ans += res[i] ans = ans[::-1] return ans n = 512462687 print(solve(n))
- How to place a thousand separator in MySQL records?
- How to format number with “.” as thousand separators, and “,” as decimal separator?
- Program to find number of square submatrices with 1 in python
- Program to find number of substrings with only 1s using Python
- Program to find Nth Fibonacci Number in Python
- Program to find reformatted phone number in Python
- Program to find higher number with same number of set bits as n in Python?
- Program to find number of sub-arrays with odd sum using Python
- Program to find equal sum arrays with minimum number of operations in Python
- Python program to find largest number in a list
- Different Methods to find Prime Number in Python Program
- Program to find number of good pairs in Python
- Program to find number of good triplets in Python
- Program to find number of distinct subsequences in Python
- Program to find number of squareful arrays in Python