python3: how do i format 2 as 02 [duplicate] - python-3.x

This question already has answers here:
Display number with leading zeros [duplicate]
(19 answers)
Closed 8 years ago.
Apologies if this is a repeat question but,
what formatting commands do i need to use if I want a single digit number to be displayed with a zero in front?
i.e. the number '2' would be displayed as '02'.
[But, I do not want any value above 10 to have extra zeros in front]
cheers

You can use this syntax:
>>> "{:0>2}".format(2)
'02'
>>> "{:0>2}".format(98)
'98'
>>> "{:x>4}".format(2)
'xxx2'
More info: Common string operations

Try:
if 0 < num < 10:
return "0" + str(single_digit)

Related

How to extract number from string in pandas? [duplicate]

This question already has answers here:
Why doesn't [01-12] range work as expected?
(7 answers)
RegEx - Match Numbers of Variable Length
(4 answers)
Closed 4 months ago.
My case is extract number between text (ex: FL-number-$) from string in File names column to Check column. Example:
2022-06-09-FR-Echecks.pdf > Return ''
2022-06-09-FR-FL-3-$797.pdf > Return 3
2022-06-09-FR-TX-20-$35149.91.pdf > Return 20
My case as below
This code I used:
dt_test['File_names_page'] = dt_test['File names'].str.extract('\-([0-99])-\$')
It only return one digit number as below:
So how to extract all number (all digit) in my case?
Tks for all attention!
Your regex pattern is slightly off. Just use \d+ to match any integer number:
dt_test["File_names_page"] = dt_test["File names"].str.extract(r'-(\d+)-\$')
You can't use a 0-99 range, you should use \d{1,2} for one or two digits:
dt_test['File_names_page'] = dt_test['File names'].str.extract(r'-(\d{1,2})-\$')
Or for any number of digits (at least 1) \d+:
dt_test['File_names_page'] = dt_test['File names'].str.extract(r'-(\d+)-\$')
NB. - doesn't require an escape
Example:
File names File_names_page
0 ABC-12-$456 12

Operand for matching any one of multiple cases [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 3 years ago.
I would like to have an if statement where the match is one of several numbers without using a large number of | bars because I have many matching cases
if number == 1 | number == 5 | number == 7
do stuff
In R, there is the operand %in% that works sort of like the following:
if number %in% [1,5,7]
do stuff
Is there a similar operand/ability in python?
Thanks
You can use something like
if number in [1, 5, 7]:

Getting the number in the ones place python3 [duplicate]

This question already has answers here:
How to check last digit of number
(10 answers)
Closed 4 years ago.
I was wondering if there was any way to get the ones place of a integer. For example, if I had the number 41, would there be any easy way to get 1 from that? Thanks!
Take modulo by 10
a = float(input('Enter a number : '))
print( a % 10)
Modulo operator return the remainder of number so any number modulo with 10 will return its ones number for more https://docs.python.org/3/reference/expressions.html

Python 3 : print float precision without zero followed [duplicate]

This question already has answers here:
Formatting floats without trailing zeros
(21 answers)
Closed 5 years ago.
For example
x = 1.23
print("%.3f" % x)
Output will be "1.230" and I want "1.23".
Is there any way to do? Thank you.
edited:
I want a function that will print floating point at limit precision and will not print zero followed if it's not reach the limit given
If you want to output up-to 3 digits but without 0 you need to format to 3 digit and rstrip zeros:
for n in [1.23,1.234,1.1,1.7846]:
print('{:.3f}'.format(n).rstrip("0")) # add .rstrip(".") to remove the . for 3.00003
Output:
1.23
1.234
1.1
1.785
Have a quick read here for python 3 formattings: https://pyformat.info/#number or here: https://docs.python.org/3.1/library/string.html#format-examples

Convert String To Time Duration In Excel [duplicate]

This question already has answers here:
Converting Time Formats in Excel
(2 answers)
Closed 6 years ago.
May I know the way to convert "1 h 49 m 57 s" to 1:49:57 in Excel.
Thanks in advance
You can use this formula to replace letters to colons then convert text to value:
=VALUE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A5," h ",":")," m ",":")," s",""))
After inserting the formula set the cell's number format to "time".

Resources