This question already has answers here:
Using variables in the format() function in Python
(3 answers)
Python string formatting on the fly
(2 answers)
Closed 4 months ago.
I'm using python formating to print some left aligned columns.
This nicely left alignes a, b and c:
out_string = '{:30} {:20} {:10}'.format(a, b, c)
Then I tried using constants, but failed with ValueError: Invalid format specifier:
ALIGNa = 30
ALIGNb = 20
ALIGNc = 10
out_string = '{:ALIGNa} {:ALIGNb} {:ALIGNc}'.format(a, b, c)
Why does it fail?
Related
This question already has answers here:
multiplication of two arguments in python [duplicate]
(6 answers)
Getting issue while trying to multiply string and integer [duplicate]
(2 answers)
Closed 3 months ago.
I want to output 5 * 2 = 10 but python output is 55!
How do I resolve this problem?
a = 0
b = 2
a = input("a? :") #(get 5 as input)
c = a * b
print (c)
This is my code.
when I input a number it repeat same number I entered two times insterd of showing multipiy it. What do I have to do to solve this?
a is a string,
so it will be
'5'*2='55'
if you want 10, you need to cast a to int.
a=int(input())
here is the link to document
https://docs.python.org/3/library/functions.html#input
This question already has answers here:
Convert integer to string in Python
(14 answers)
String formatting: % vs. .format vs. f-string literal
(16 answers)
TypeError: Must be str, not int error occuring
(2 answers)
Closed 1 year ago.
I need to do this:
create the function named exampleThree, that will have three input parameters.
return the single string with element separated with ...
So for example, the text would need to go from: "I like dogs" ---> "I...like...dogs"
This is what I currently have but it is not working. It is telling me that a b and c "must be str, not int"
def exampleThree(a,b,c):
return a + "..." + b + "..." + c
Can I get assistance with this? Thanks.
Python is not a strongly typed language, thus whatever you provide the function as parameters can be easily miss-typed.
S you have 2 options here:
specify the data type in the function signature
my_func(a:str, b:str, c:str):
return a + "..." + b + "..." + c
cast parameters to str
my_func(a, b, c):
return str(a) + "..." + str(b) + "..." + str(c)
the second option can result in exceptions if you try to cast objects
#a function
def exampleThree(a,b,c):
return f"{a}....{b}....{c}"
# printing
print(exampleThree(25,30,40))
#an output
25....30....40
This question already has answers here:
x, = ... - is this trailing comma the comma operator? [duplicate]
(2 answers)
Meaning of using commas and underscores with Python assignment operator? [duplicate]
(4 answers)
Closed 3 years ago.
What does this code do?
f = [1]
x, = f
x is set to 1 after doing this, but I cannot figure out what the , = does.
This question already has answers here:
Multiple assignment and evaluation order in Python
(11 answers)
Swap 2 values of 2 variables without using a third variable; python
(5 answers)
Is there a standardized method to swap two variables in Python?
(8 answers)
Closed 5 years ago.
Why does
a = 3
b = 5
a,b = a + b,a
print a,b
-------
8 3
differ from
a = 3
b = 5
a = a + b
b = a
print a,b
------
8 8
Can someone explain how Python interprets the assignments differently? Does Python still keep the old value of a in the first paragraph of code, when a is assigned to a + b? (a= a+b)
Also, does these assignment properties change from Python 2 to Python 3?
This question already has answers here:
How to display a byte array as hex values
(5 answers)
Closed 4 years ago.
In my code I receive a bytearray with the value 0x41 as b'\x41'. Printing this value gives b'A'. How can I print it in such a way that i get the result b'\x41'. (Python 3.7)
Testcode:
print("b'\x41'")
Unfortunately, if you don't like the default display, you have to generate it yourself:
>>> b = b'\x41\x42\x43'
>>> print(b)
b'ABC'
>>> print("b'" + ''.join(f'\\x{c:02x}' for c in b) + "'")
b'\x41\x42\x43'