This question already has answers here:
Python - decimal places (putting floats into a string)
(3 answers)
Closed 3 years ago.
I've a code in perl,
$abc = sprintf("%.5f", 15.4595255213733);
print ($abc) // 15.45953
I'm looking for an equivalent to sprintf in python? or how would I be able to achieve this
This is called string formatting.
print('{:.5f}'.format(abc))
The "old" way to do this would be:
print('%.5f' %abc)
Related
This question already has answers here:
What do backticks mean to the Python interpreter? Example: `num`
(3 answers)
Meaning of the backtick character in Python
(2 answers)
Closed 1 year ago.
Lots of old python code I look in has this ` symbol around a lot of stuff, what does it do? Now it is not considered valid syntax, obviously.
And I don't think it is just another string identifier, its sometimes wrapped around functions in the code I'm looking at.
Any help will be appreciated.
This question already has answers here:
How to change a string into uppercase?
(8 answers)
Closed 2 years ago.
What is the best pythonic way to covert, a string as '11-2020' to 'NOV-2020' in Python.
I tried below code :
print(datetime.datetime.strptime(date, '%m-%Y').strftime('%b-%Y'))
But getting output like : Nov-2020 (I want Nov to be in caps)
print(datetime.datetime.strptime(date, '%m-%Y').strftime('%b-%Y').upper())
#NOV-2020
This question already has answers here:
Reference - What does this regex mean?
(1 answer)
What exactly is a "raw string regex" and how can you use it?
(7 answers)
Python3 regex on bytes variable [duplicate]
(2 answers)
Closed 2 years ago.
my_address = re.search(rb'\x80\xC9.{4}\x94\x12\x8A\x41\x08', bytes).start() + 5
Read the doc but I'm a bit confused.
My guess is
\x80\xC9\BYTE\BYTE\BYTE\BYTE\BYTE\x94\x12\x8A\x41\x08\BYTE\BYTE\BYTE\BYTE\BYTE\my_address\
Thanks
This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 3 years ago.
I need a simple and effective algorithm to reverse a string (for example: WelCome to emoCleW ).
I tried a loop:
s=input("Enter String To Be Reversed:")
for i in range(len(s)+1,-1,-1):
print(str1[0:i])
but this didn't work for me
Like this:
s=input("Enter String To Be Reversed:")
print(s[::-1])
This question already has answers here:
How can I print bold text in Python?
(17 answers)
Closed 6 years ago.
I'm aware of the this solution and this solution when using string concatenator +. However I couldn't find how to do it with the new printing style (more details here), e.g. print '{:10s}'.format(str).
The same ANSI escape sequences work just fine.
print('\033[1m{:10s}\033[0m'.format('foo'))