-5

I'm trying to get a "Hello World" string from two variables (hello) (world). Can someone find the problem here, I've used the plus operator.

This is a screenshot of the lesson (concatenation) on pycharm.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Lua
  • 9
  • 1
  • 3
  • 1
    Since you're screenshot doesn't show (and you shouldn't use screenhots, just post the code and traceback), all I know is that you're using a plus operator and that something is wrong... I can only imagine you tried to add 1/0 to itself and got a black-hole exception – en_Knight Mar 20 '15 at 04:41
  • The lesson is asking me to solely type/modify the contents of the box. I haven't touched anything else (in some cases I did, it didn't help though), there must be a correct, clear answer. Why is it saying "Use + operator", when you can clearly see the plus sign between the hello and world. Am I suppose to add the addition sign elsewhere, or is the sign not suppose to be there in the first place. You can see from the output that It's not printing/using the hello/world variables. Try the challenge yourself: download pycharm educational editon and navigate to the introduction to python. Thanks :) – Lua Mar 20 '15 at 08:20
  • Does this answer your question? [Which is the preferred way to concatenate a string in Python?](https://stackoverflow.com/questions/12169839/which-is-the-preferred-way-to-concatenate-a-string-in-python) – bad_coder Aug 28 '21 at 17:48

6 Answers6

1

First of all, you should be more precise to what you are trying to ask. Secondly I am just trying to help you with what I've understood,

x = 'hello'
y = 'world'

print (x, y)

output: hello world

Not sure if you are trying to achieve that or something else.

Hafiz Temuri
  • 3,882
  • 6
  • 41
  • 66
  • Specific question: Why is the "Use + operator" there? You can see, I've used it. Whatever I try to do; it either comes back with two errors: 'USE + OPERATOR' or 'USE ONE-SPACE STRING IN CONCATENATION'. When I add the quotation marks; it redirects me to the + operator error. – Lua Mar 20 '15 at 08:24
  • when I'm using the x, y method. The code doesn't meet certain requirements: so more errors here. – Lua Mar 20 '15 at 08:25
  • use this instead then, **print (x+" "+y)** – Hafiz Temuri Mar 22 '15 at 06:23
1

I think the recommended answer would be something along these lines:

hello="Hello"
world='World'

hello_world=hello + ' ' + world
print=hello_world

Run output: Hello World
Lua
  • 9
  • 1
  • 3
0

you can print like this

x="Hello"
y="world"
print x,y

or

print x+y
Benjamin
  • 2,257
  • 1
  • 15
  • 24
  • generally try to avoid `print x+y` because it gets to be a bit of a hassle later on when you start printing numbers along with strings and whatnot. you're usually better off using `print x,y` usually. – Ben Schwabe Mar 20 '15 at 05:08
  • Done that. It prints, but the lesson says it's wrong. – Lua Mar 20 '15 at 08:25
  • I went on rosseta code; in search of a python example for string concatenation. Here it is: s1 = "hello", print s1 + " world". So s1 is the variable, and you can see that it has no quotation marks!, when being used. Why is Pycharm saying use one space string in concatenation? – Lua Mar 20 '15 at 08:35
  • I notice that when printing; there are no brackets. – Lua Mar 20 '15 at 08:41
0

I've solved it!. The rosetta code, helped. First off, i decided not to use any brackets in it. I also ignored the quotation error, as the example on the rosseta code didn't contain any quotations. I can conclude that the program is really buggy, I managed to pass the lesson by adding another variable. Here it goes:

hello = "Hello"
world = "World"
s1 = " "

hello_world = hello + s1 + world
print (hello_world)

Run output: Hello World

I still don't know what it expected for me to place in the box. The instructions said used hello and world (variables) to get a "Hello World" string. In all my attempts; i got to HelloWorld. I added a space, and it worked.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Lua
  • 9
  • 1
  • 3
0

The solution is actually quite simple. If you print what you wrote you will get HelloWorld (as you have already mentioned). All that is missing is an extra added space before ' World' or after "Hello " and your code should work. I think you are overcomplicating it by adding an extra variable for a space.

0

All you have to do is add a space in the string hello + '(add a space here)' + world If you just press the '' button it won't automatically add the space in between. That's what it means by "use a one space string"