0

I have written a code that returns a phrase depending on the length of a name. Problem is, I can't submit it unless i fix this tiny error.

Here's what I've written:

name = input('Enter your name: ')

if len(name) <= 3:

  print ('Hi',name,', you have a short name.')

elif len(name) > 8:

  print ('Hi',name,', you have a long name.')

elif len(name) >= 4:

  print ('Hi',name,', nice to meet you.')

For example: When i type in a 3 letter name such as 'Lin' it reruns the following

"Hi Lin*(space)*, you have a short name."

I wish to get rid of the (space) and have it return:

"Hi Lin, you have a short name."

I think the error lies with my concatenation and it automatically ads a space after the comma.

Junuxx
  • 14,011
  • 5
  • 41
  • 71
user2677100
  • 17
  • 1
  • 2

4 Answers4

0

The print function prints a space between its arguments. You can explicitly concatenate the strings to control the separation:

print('Hi ' + name + ', you have a short name.')

or use string formatting:

print('Hi {}, you have a short name.'.format(name))
user2357112
  • 260,549
  • 28
  • 431
  • 505
0

For your input is is also possible to get rid of the whitespaces with the strip() function

name = name.strip()

How to trim whitespace (including tabs)?

Community
  • 1
  • 1
lordkain
  • 3,061
  • 1
  • 13
  • 18
0

You're are right, print(x, y, z) will sequentially print x, y and z with a space inbetween. As you suggest, concatenate your name to your greeting, and then print the greeting:

name = input('Enter your name: ')
if len(name) <= 3:
  greeting = "Hi {}, you have a short name.".format(name)

elif len(name) > 8:
  greeting = "Hi {}, you have a long name.".format(name)

elif len(name) >= 4:
  greeting = "Hi {}, nice to meet you.".format(name)

print(greeting)
Nick Burns
  • 973
  • 6
  • 4
0

You can concatenate strings in Python like this:

print('Hello ' + name + ', you have a wonderful name!')

Or, without the parenthesis:

print 'Hello '  + name + ', you have a wonderful name!'

There's various other tricky things you can do, but for something like this just having '+' signs will do.

Phillip Carter
  • 4,895
  • 16
  • 26