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'))
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:
What is the difference between a "line feed" and a "carriage return"?
(4 answers)
What does "\r" do in the following script?
(4 answers)
Closed 2 years ago.
I am a newbie in the field of python. I recently came across \r (carriage return). It seems like it performs the same task as \n. Can anyone tell me the difference between \r and \n and specify the operation of \r?
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)
This question already has answers here:
How to replace multiple substrings of a string?
(28 answers)
Closed 4 years ago.
I currently have the below Python code which works well:-
caseURL = r"\\mydomain\abc\lp_t\GB\123456\Original Format"
caseURL = caseURL.replace("lp_t", "lp_i")
caseURL = caseURL.replace("Original Format", "1")
This works fine as said and carries out the below conversion:-
\\mydomain\abc\lp_t\GB\123456\Original Format
\\mydomain\abc\lp_i\GB\123456\1\
This however just seems a bit clumsy. Is there a more pythonesque way to perform these two segment replacements?
Thanks
A similar post already exists:
How to replace multiple substrings of a string?
You can pick one answer from multiple options in the above post.