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
Related
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?
This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 5 years ago.
I am trying to write a function that takes a string and reverses the words in that string, and then returns the string. I have written this code but when I execute it nothing happens. A blank appears.
def reverse(tex):
new_w = " "
for i in range(0, len(tex)):
new_w += text[len(tex) - 1 - i]
return new_w
>> name = "pawan"
>> name[::-1]
>> "nawap"
This is better, isn't it?
This is called slicing.
Here's the syntax for slicing:
[ first char to include : first char to exclude : step ]
EDIT: I suggest you take a look at this link.
This question already has answers here:
Why does map return a map object instead of a list in Python 3?
(4 answers)
Getting a map() to return a list in Python 3.x
(11 answers)
Closed 5 years ago.
How to Convert a string like '123' to int 1,2 and 3 so that I can perform 1+2+3. I am new to python. Can you please help me? I am not able to split the list. I don't think splitting the string will be of any use as there are no delimiters. Can you help me to understand how can this string elements be separated and treated as intergers?
x = "123"
s = 0
for a in x:
s = int(a) + s
This question already has answers here:
How to print without a newline or space
(26 answers)
How can I print variable and string on same line in Python? [duplicate]
(18 answers)
Closed 5 years ago.
I'm new to python and learning how to code.
I'm printing last element of my list and sum of the list as-
print list[-1],sum
But the output is separated by " " and not separated by ",".
Any idea how to separate it by comma?
I'm using Python 2.7
Include it in quotes, like this:
print str(list[-1]) + "," + str(sum)
Enclosing them in str() is unnecessary if list[-1] and sum are strings.
In general, symbols are interpreted as Python symbols (for example, names like sum are interpreted as variable or function names). So whenever you want to print anything as is, you need to enclose it in quotes, to tell Python to ignore its interpretation as a Python symbol. Hence print "sum" will print the word sum, rather than the value stored in a variable called sum.
You'll have to compose that together into a string. Depending on what version of Python you're using, you could either do:
print "{},{}".format(list[-1], sum)
or
print "%s,%s" % (list[-1], sum)
If you were using Python3.6+, there would be a third option:
print(f"{list[-1]},{sum}")
Use the sep keyword argument:
print(list[-1], sum, sep=',')
You can use str.format() and pass whatever variables you want to get it formatted, for example:
x = 1
z = [1, 2, 3]
y = 'hello'
print '{},{},{}'.format(x, z[-1], y)
# prints: 1,3,hello
This question already has an answer here:
How can I concatenate str and int objects?
(1 answer)
Closed 4 years ago.
I get this error when trying to take an integer and prepend "b" to it, converting it into a string:
File "program.py", line 19, in getname
name = "b" + num
TypeError: Can't convert 'int' object to str implicitly
That's related to this function:
num = random.randint(1,25)
name = "b" + num
name = 'b' + str(num)
or
name = 'b%s' % num
Note that the second approach is deprecated in 3.x.
Python won't automatically convert types in the way that languages such as JavaScript or PHP do.
You have to convert it to a string, or use a formatting method.
name="b"+str(num)
or printf style formatting (this has been deprecated in python3)
name="b%s" % (num,)
or the new .format string method
name="b{0}".format(num)
Python 3.6 has f-strings where you can directly put the variable names without the need to use format:
>>> num=12
>>> f"b{num}"
'b12'
Yeah, python doesn't having implicit int to string conversions.
try str(num) instead
name = "b{0:d}".format( num )