Convert int to string in fixed no of characters [duplicate] - python

This question already has answers here:
How do I pad a string with zeroes?
(19 answers)
Closed 7 years ago.
I am writing int to a file. if the number is 0, then i want to write it in file as 0000. currently
o.write(str(year))
writes only 0.
How can it be done?

try this: (the essence is using zfill to show the number of zeros you want in the most succinct way)
if int(my_number_as_string) == 0:
print my_number_as_string.zfill(4)

Following function will help you to pad zeros
def add_nulls2(int, cnt):
nulls = str(int)
for i in range(cnt - len(str(int))):
nulls = '0' + nulls
return nulls
Output
>>> add_nulls2(5,5)
'00005'
>>> add_nulls2(0,5)
'00000'
>>>

Related

How to increment numaric values only of alphanumeric values [duplicate]

This question already has answers here:
How do I create an incrementing filename in Python?
(13 answers)
convert number to formatted string in python with prefix 0s
(3 answers)
Closed 2 years ago.
proId = 'nam001'
I want to increment numeric values only. That should be like this 'nam002', 'nam003'.
How to do this?
Just because you asked nicely :D
You can use the simplest for loop, with str.zfill to pad with zeroes, then add it to the 'nam' prefix like this:
for i in range(1,11):
proId = 'nam' + str(i).zfill(3)
print(proId) # or do whatever you need with it...
Output:
nam001
nam002
...
nam009
nam010

How to format '7' to produce '007' [duplicate]

This question already has answers here:
How to pad a string with leading zeros in Python 3 [duplicate]
(5 answers)
Closed 2 years ago.
How would you format '7' to print '007' here:
>>> number = 7
>>> print(number)
7
Ideally, there'd something similar to always showing these decimal places?:
>>> number = 7
>>> print("{:.2f}".format(number))
7.00
I've searched online for a similar solution to this but can't seem to find anything in this format...
Thanks for the help!
You just need to cast 7 using str() and finally use zfill to add leading zeros:
Return a copy of the string left filled with ASCII '0' digits to make
a string of length width. A leading sign prefix ('+'/'-') is handled
by inserting the padding after the sign character rather than before.
The original string is returned if width is less than or equal to
len(s).
str(7).zfill(3)
>>> '007'
try this
print("{0:0=3d}".format(number))

How to turn -1.06267582739e+11 into -106267582739.0 in python? [duplicate]

This question already has answers here:
How to suppress scientific notation when printing float values?
(16 answers)
Closed 4 years ago.
How to suppress scientific notation from a float value in python. Here I tried the following code but it's not working
r_val[v].append('%.2f' % val.get("closing_balance"))
Thanks in advance
Using format(x, '.#f')
consider this snippet:
x = 0.000000235
print(x)
2.35e-07
print (format(x, '.9f'))
0.000000235
Or, to go closer to your question:
y = -1.06267582739e-11 # note I changed '+' to '-' since '+' is is just represented as a regular float
print(y)
-1.06267582739e-11
print(format(y,'.22f'))
-0.0000000000106267582739

Python: How to convert float to string without `.0`? [duplicate]

This question already has answers here:
Formatting floats without trailing zeros
(21 answers)
Closed 6 years ago.
I want to extract telephone number from *.xls with module xlrd using Python, but it shows like this:'16753435903.0'.
Here's the code:
opensheet = openxls.sheet_by_name(u'sms')
sheetrows = opensheet.nrows
for rownum in range(1, sheetrows):
rowvalue = opensheet.row(rownum)
execinfo = ""
for colnum in range(1,5):
execinfo += "'"+str(rowvalue[colnum].value)+"',"
The key part:
str(rowvalue[colnum].value)
How can I get the well format telephone number without .0?
This might be a very simple workaround: convert the number to an int before converting it to a string?

Padding zeroes to left of string (python)? [duplicate]

This question already has answers here:
How do I pad a string with zeroes?
(19 answers)
Closed 7 years ago.
The length of the string needs to be 5 characters. When the string is "1" it needs to be returned as "00001", when the string is "10" it needs to be returned as "00010" and so on. I'm wondering how to do this using loops?
If you want to use for-loops, you can solve the problem like so:
def addPadding(str):
output = ''
# Prepend output with 0s
for i in range(5 - len(str)):
output += '0'
output += str
return output
print(addPadding('10'))
>> 00010
print(addPadding('1'))
>> 00001
If you can't use string formatting or arrays or anything besides integer operators, you should be able to figure it out using division and a loop.
Is 10 divisible by 10000?
Is 10 divisible by 1000?
Is 10 divisible by 100?
etc.
Try typing 10/10000 in your python interpreter. What's the result? :)

Categories