4

How can I print my strings so that there are no spaces between each output.

name_input = str(input("Please enter your name"))
name_input = name_input.strip()
name_input = name_input.lower()
first_letter = name_input[0]
first_space = name_input.find(" ")
last_name = name_input[first_space:]
last_name_3 = last_name[0:4]
random_number = random.randrange(0,999)
print("*********************************************")
print("Username Generator")
print("*********************************************")
print(first_letter + last_name_3, random_number)`

Incorrect output: b fir 723

what I require: bfir723

1

5 Answers 5

11

use the separator parameter to the print function, to remove the space by passing in an argument with no space.

Like this:

print(first_letter, last_name_3, random_number, sep='')

The default separator is a space. Specification is here:

https://docs.python.org/3/library/functions.html#print

Sign up to request clarification or add additional context in comments.

Comments

8

You need to use strip() function for this:

print(first_letter.strip() + last_name_3.strip() + str(random_number).strip())

5 Comments

That did remove the space between the strings however the strip function did not remove the spaces between the numbers. Current output bfir 723
@BaileyFY: I've edited my answer to trim numbers as well.
That doesn't seem to fix the problem
print(first_letter.strip() + last_name_3.strip(), (random_number).strip()) AttributeError: 'int' object has no attribute 'strip' Error message
You've missed out str in str(random_number).strip() part.
2

You don't need strip() and sep=''

You only need one or the other, but sep='' is cleaner and more beautiful

Comments

1

You could also just do

print(first_letter + last_name + str(random_number))

Comments

0

It turns out a combination of .strip() and ,sep='' was required to print correctly:

print(first_letter.strip() + last_name_3.strip(), random_number, sep="")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.