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))
Related
This question already has answers here:
Understanding slicing
(38 answers)
Closed 2 years ago.
Assuming the string has at least one character in it. If the string length is odd, then you may assume it returns (𝑛−1)/2 characters where n represents how many characters are in the original string.
For example:
'small' => 'sm'
I would also like to write another function that returns the 2nd half of a string.
You can just do an integer division (//) to get the integer value of the division to get the desired (n-1)/2 value for odd n. Therefore, having the following:
>>> my_string = "small"
>>> print(my_string[:len(my_string)//2])
sm
You could do the similar thing with using math.floor to be more explicit, but result is the same.
This question already has answers here:
Understanding slicing
(38 answers)
Closed 3 years ago.
If I have a string of variable length and I want to return only first 3 characters of the string.
str[:3]
this works but I want to know if the string is of lesser length, suppose 2, "ab" or just "a", will this slicing work for that too?
Thanks in advance
Python will return at most :n characters:
'a'[:3] will simply return 'a'. ''[:3] returns ''.
You could have tested for yourself in less time than it would have taken to open your browser.
But yes.
This question already has answers here:
Convert to binary and keep leading zeros
(10 answers)
Converting integer to binary in python
(17 answers)
How to pad a numeric string with zeros to the right in Python?
(3 answers)
Closed 4 years ago.
In this example...
temp = 0b110
print temp
temp.str(temp).zfill(8)
print temp
the output is...
0b110
0000b110
How do I add the zeros so I see an eight bit binary output, 0b00000110?
If you're calling the bin function, or using a format string with #b, or whatever, the resulting string has 0b on the front, so it's too late to zero-fill, unless you want to do something hacky like pull it off, zero-fill to n-2 characters, then put it back on.
But if you just do the zero-filling before (or while) adding the prefix rather than after, it's easy. For example:
>>> temp = 0b110
>>> format(temp, '#010b')
'0b00000110'
The docs explain this in detail, but the short version is:
# means "alternate form", which gives me the 0b prefix
0 means zero-pad
10 means width of 10 (including the 0b)
b means binary
This question already has answers here:
Rounding a number in Python but keeping ending zeros
(6 answers)
Closed 6 years ago.
So let's say I have this code:
num = 1.29283
round(num, 2)
That rounds to 1.29, but if I do this:
num = 1.30293
round(num, 2)
That rounds to 1.3. I want to know if there is a way to have it round to 1.30; I know it is the same number, but I need it to print 1.30.
You can use string formatting for this. A number in python does not have such a thing as trailing zeros. So your question only make sense for strings.
Example:
>>> num = 1.30293
>>> "{:.2f}".format(num)
'1.30'
The .2f says that this is a float (f) and that you want two digits after the point .2. Read more about string formatting here
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? :)