0

How to add space between a variable with an integer value and a variable with a string value when printing? What is str()?

Hey, can you explain me why in: a=200 b="overdue" print(€+a+" "+b) #Output: €200overdue the space between a and b is not considered? Also, can you explain me what does str() mean for the computer?

7th Jan 2024, 2:35 AM
Vincenzo
Vincenzo - avatar
14 ответов
+ 6
Wong Hei Ming , just to mention that there is an issue in your mentioned print statement. it should be: print(f"€{a} {b}") # position of closing quote was print(f"€"{a} {b}) ^
7th Jan 2024, 7:06 AM
Lothar
Lothar - avatar
+ 3
Hey Vincenzo! In the code you shared, the space between `a` and `b` is not considered because the `+` operator is used for concatenating strings. So when you use `+` to combine `a`, `" "`, and `b`, it joins them together without any spaces. Now, about `str()`, it's a function in Python that converts a value into a string. It's useful when you want to concatenate a non-string value with a string. For example, if you have a number like `10` and you want to combine it with a string like `"apples"`, you can use `str(10)` to convert the number into a string and then concatenate it with `"apples"`. Hope that helps! Let me know if you have any more questions. 😊
7th Jan 2024, 3:08 AM
Brayden Ames
+ 3
Vincenzo str() means that you are converting something rather an integer or a float to a string x = 15 Is an integer y = str(x) Is a str of x or "15" a = 200 a = str(a) becomes "200" string not 200 integer
7th Jan 2024, 3:08 AM
BroFar
BroFar - avatar
+ 3
It is the correct statement. print("€" + str(a) + " " + b) Adding space between + sign makes the code easier to read. You need to put the Euro Sign inside quote in order to print it. Maybe you typed "" instead of " " when you tested your code, your code above has a space between them.
7th Jan 2024, 4:28 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 3
Vincenzo while you have a very simplified print idea: print("€"+str(a)+" "+b) to maintain spacing and such this might be a better approach a = 200 b = "overdue" print(f"%s{str(a)} {b}" %(u"\N{euro sign}")) Output: €200 overdue By using the f-string or print(f" you eliminate the need to use the plus sign over and over to concat pieces into one string.
7th Jan 2024, 4:57 AM
BroFar
BroFar - avatar
+ 3
Vincenzo It is simply: print(f"{a} {b}") you don't need to {" "} for a space
7th Jan 2024, 6:21 AM
BroFar
BroFar - avatar
+ 3
Vincenzo , When you put code in a comment, be sure to copy and paste from the playground. Don't retype it by hand, or new errors could get in. Your question was about space between variables of different types. If you list them as separate arguments, print will automatically insert a space. a = 200 # int b = "Motels" # str print(a, b) # 200 Motels When you don't want a space, you can concatenate. Only strings can be concatenated. str() turns anything into a string. currency = "
quot; # str amount = 49.99 # float product = "Hat" # str print(currency + str(amount), product) # $49.99 Hat But the easiest is an f-string, because you can mix spaces and no spaces and punctuation and variables and literals all in one go. I don't think you're to that part of the course though. payments = "three" # str amount = 19.95 # float print(f"Only {payments} payments of ${amount}, and it's yours!") # Only three payments of $19.95, and it's yours!
7th Jan 2024, 10:19 PM
Rain
Rain - avatar
+ 3
To add space between a variable with an integer value and a variable with a string value when printing, you can concatenate them with a space in between. Here's an example: ```python a = 200 b = "overdue" print("€" + str(a) + " " + b) # Output: €200 overdue ``` In this example, `str(a)` converts the integer variable `a` into a string so that it can be concatenated with the other string variable `b`. In your provided code: ```python a = 200 b = "overdue" print(€ + a + " " + b) # Output: €200overdue ``` The issue is that you are trying to concatenate a numeric value (`€`) directly with an integer (`a`). To resolve this, you can convert the integer to a string using `str(a)` as shown in the first example. Regarding `str()`, it's a function in Python that converts its argument into a string representation. For example: ```python num = 42 str_num = str(num) print(type(str_num)) # Output: <class 'str'> ``` Here, `str_num` will be a string representation of the integer `num`. The `str()` function is commonl
8th Jan 2024, 2:21 AM
𝐀𝐲𝐞𝐬𝐡𝐚 𝐍𝐨𝐨𝐫
𝐀𝐲𝐞𝐬𝐡𝐚 𝐍𝐨𝐨𝐫 - avatar
+ 2
BroFar Your code is overcomplicated. print(f"€"{a} {b}) will do the job. Results in €200 overdue Vincenzo I wrote a code describes how to format a string in Python. You can take a look if interested. https://sololearn.com/compiler-playground/c81oGO8Ygckc/?ref=app
7th Jan 2024, 5:44 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
You cannot mix f-string with str.format(). Use either one of these print(f"a{a}{a0[0]} b{b}{b0[0]}") print("a{}{} b{}{}".format(a, a0[0], b, b0[0]))
7th Jan 2024, 7:46 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Thank you all, buddies! By the way, I didn't know about the f-string. This is gonna help me synthesize a lot my codes!
7th Jan 2024, 6:07 AM
Vincenzo
Vincenzo - avatar
+ 1
I noticed that if you let the computer type for you the quotation marks, you need to add space between them in order to make them count as a blank space. so it's: print("a"+" " +"b") #and not print("a"+" "+"b") #then there is this good option print(f"{a}{b}") #better with the automatic {} tho
7th Jan 2024, 6:15 AM
Vincenzo
Vincenzo - avatar
+ 1
Wong Hei Ming I see. Thanks 😁
7th Jan 2024, 1:59 PM
Vincenzo
Vincenzo - avatar
0
Wong Hei Ming I tried the .format() string in a code but idk what I did wrong not to get this output: a11 b11 Can you help me? The code: https://sololearn.com/compiler-playground/cBJpznrb2nQO/?ref=app
7th Jan 2024, 7:26 AM
Vincenzo
Vincenzo - avatar