This question already has answers here:
In Python format (f-string) strings, what does !r mean? [duplicate]
(1 answer)
How to left align a fixed width string?
(8 answers)
Closed 1 year ago.
I was following a tutorial and found this.
print(f"{self.name}: {card!r:<3} ", end="")
And I have no idea what the !r:<3 means. Google wouldn't give me relevant results because of all the symbols.
You can find a description of the format of f-strings here and of the formatting language here.
!r:<3 uses repr to format the value, left aligned with a minimum width of 3 (padded with spaces by default).
Related
This question already has answers here:
What is the difference between re.search and re.match?
(9 answers)
Closed 1 year ago.
See title. The behavior here is counterintuitive. I think maybe I am missing some flags or something? Why does the regex z not match the string az?
The reason is that match only matches the beginning of a string. Must use search to do the thing that match would do in all other programming languages.
Sorry to throw shade at you Python, but you're the odd one out here.
This question already has answers here:
How to replace two things at once in a string?
(6 answers)
using .replace to replace more than one character in python [duplicate]
(4 answers)
Closed 2 years ago.
I have a question regarding my code below:
Input: A DNA string Pattern (ex: 'AAAACCCGGT')
Output: The complementary string (ex: 'TTTTGGGCCA')
def Complement(Pattern):
comPattern=Pattern.translate(str.maketrans({'A':'T','T':'A','G':'C','C':'G'}))
return comPattern
I tried using str.replace() method multiple times for above problem, but it did not work. Any idea why?
This question already has answers here:
Python format throws KeyError
(1 answer)
How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?
(23 answers)
Closed 3 years ago.
I just want the following outcome.
But I get KeyError: '"msg_body"'.
input:
text="text"
uid="uid"
input = '{"msg_body":{input_text}, "user_id":{input_uid}}'.format(input_text=text, input_uid=test)
wanted output:
'{"msg_body":"text", "user_id":"uid"}'
Single or double quotations must be exactly how it is above.
Thanks
This question already has answers here:
How can I selectively escape percent (%) in Python strings?
(6 answers)
What is %% for in Python? [duplicate]
(1 answer)
Closed 4 years ago.
I came across a problem when I was learning python.
print('test%d, %.2f%%' % (1,1.4))
however, it has an error.
ValueError: incomplete format
But if I execute like this:
print('test%d, %.2f%%' % (1,1.4))
test1, 1.40%
It works and prints the '%'. But I don't know why? Can someone help me? Thanks.
Since % is used as a special character in (old C-style) format strings, you have to use %% to print a literal percent sign.
You need to look into c-style string formatting. %% is a reference to this series of string formatting commands.
The following page:
https://docs.python.org/3.4/library/string.html
has a "String formatting mini-language" section that answers your question in meticulous detail.
This question already has answers here:
How can I selectively escape percent (%) in Python strings?
(6 answers)
Closed 6 years ago.
In the sec2time() Python function provided by Lee he uses a syntax I'm struggling to understand:
pattern = '%%02d:%%02d:%%0%d.%df' % (n_msec+3, n_msec)
What is the %%here and how does it affect the outcome?
The % in that string introduces replaceable parts as at the end %d.%df. If you want a % in the output you have to do something special, in this case use %%
After these substitustions the resulting pattern will look like:
'%02d:%02d:%0123.120f'
which, among other things an be used for further substitution.
In the documentation, at the bottem of the second table in that section, it states:
'%' No argument is converted, results in a '%' character in the result.