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? :)
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:
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))
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:
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'
>>>
This question already has answers here:
Formatting floats without trailing zeros
(21 answers)
Closed 8 years ago.
I have written a code that will add up the values in a tuple and calculate the average:
def average(values):
return sum(values[0:]) / len(values[0:])
However, I get an unwanted floating point, like 2.0 instead of 2. How do I eliminate this, but still manage to get the correct average should the average not be an integer?
You may try like this:
if (yournumber).is_integer():
print int(n)
else
print (n)