This question already has answers here:
What is the syntax rule for having trailing commas in tuple definitions?
(10 answers)
Closed 10 years ago.
I am new to python and Django and am trying to determine how the following code works:
return HttpResponseRedirect(reverse('poll_results', args=(p.id,)))
Specifically the 2nd argument of the reverse function. It looks like it is setting the parameter of args equal to a tuple. Why do I need an extra comma?
(p.id) is just p.id in parentheses, (p.id,) is a single-element tuple.
Parenthesized forms in docs
The trailing comma is required if a tuple only has one item to differentiate a tuple from stylistic parenthesis.
Similar questions:
Python tuple comma syntax rule
Why does adding a trailing comma after a string make it a tuple
Related
This question already has answers here:
How do I remove a substring from the end of a string?
(23 answers)
Closed last year.
I was surprised about strip Python method behavior:
>>> 'https://texample.com'.strip('https://')
'example.com'
It was not obvious, because usually I use strip with an one-char argument.
This is because of
The chars argument is a string specifying the set of characters to be removed
(https://docs.python.org/3/library/stdtypes.html#str.strip).
What is the best way to delete a "head" of a string?
you have 3 options:
use string.replace instead of string.strip
startswith method:
if line.startswith("'https://"):
return line[8:]
split:
if "/" in line:
param, value = line.split("/",1)
This question already has an answer here:
Why does printing a tuple (list, dict, etc.) in Python double the backslashes?
(1 answer)
Closed 1 year ago.
enter image description here
is there a way to print single backslash within list?
Regarding the first version of your question, I wrote this:
First, this expression x='\' isn't right in Python in python. you should rather puth it this way: x='\\', since back slash is a special character in python.
Second, try this:
l=['\\'] print(l)
This will print: ['\\']
But when you execute this: print(l[0]), it renders this '\'. So basically, this ['\\'] is the way to print a backslash within a list.
This question already has an answer here:
What does it mean in documentation when arguments are in square brackets? [duplicate]
(1 answer)
Closed 1 year ago.
What does the brackets around input arguments mean?
Consider this example:
cv.boxPoints(box[,points])
example doc link
What confuses me the most is the comma inside the bracket of the second input argument points. Why is it represented as [,points]. Any information on this would be greatly appreciated.
Thanks!
It's a common convention in documentation to denote optional arguments with square brackets. That documentation is telling you that the second argument to boxPoints is optional. It has no syntactic meaning in Python; you can call the function with one or two arguments like normal.
This question already has answers here:
How to create a "singleton" tuple with only one element
(4 answers)
Closed 6 years ago.
x=([1,2,3])
type(x)= List
x=([1,2,3],[4,5,6])
type(x)=tuple
why does the type change?
The proper syntax for creating a tuple with only one item is to follow the item with a comma:
x=([1,2,3],)
which for this example will in fact give
type(x)=tuple
Reference the official Python 2 documentation
which states (quote)
A special problem is the construction of tuples containing 0 or 1
items: the syntax has some extra quirks to accommodate these. Empty
tuples are constructed by an empty pair of parentheses; a tuple with
one item is constructed by following a value with a comma (it is not
sufficient to enclose a single value in parentheses).
This question already has answers here:
Why do Python function docs include the comma after the bracket for optional args?
(5 answers)
Closed 6 years ago.
Since the first times I started to study Python I met many schematic codes such as
pickle.dump(obj, file[, protocol])
Now in this example I can understand the meaning of the first comma, as it separate two different arguments to be inserted in a method, but I don't understand the second comma that is located after a square bracket.
Is there anyone who can explain me the meaning of this comma?
it's a common notation for indicating the next argument is optional. so you could write:
pickle.dump(obj, file)
or you could write:
pickle.dump(obj, file, protocol)
if you see angle brackets like <foo>, that is used to indicate the argument is required.