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?
14 Antworten
+ 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})
^
+ 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. 😊
+ 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
+ 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.
+ 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.
+ 3
Vincenzo
It is simply:
print(f"{a} {b}")
you don't need to {" "} for a space
+ 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!
+ 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
+ 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
+ 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]))
+ 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!
+ 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
+ 1
Wong Hei Ming
I see. Thanks 😁
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