This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 6 years ago.
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
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
You need to use strip() function for this:
print(first_letter.strip() + last_name_3.strip() + str(random_number).strip())
You don't need strip() and sep=''
You only need one or the other, but sep='' is cleaner and more beautiful
You could also just do
print(first_letter + last_name + str(random_number))
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="")
Related
This question already has answers here:
Difference between using commas, concatenation, and string formatters in Python
(2 answers)
Closed 2 months ago.
What is the difference between these two pieces of code?
name = input("What is your name?")
print("Hello " + name)
name = input("What is your name?")
print("Hello ", name)
I'm sorry if this question seems stupid; I've tried looking on Google but couldn't find an answer that quite answered my question.
In this example, you have to add an extra whitespace after Hello.
name = input("What is your name?")
print("Hello " + name)
In this example, you don't have to add an extra whitespace (remove it). Python automatically adds a whitespace when you use comma here.
name = input("What is your name?")
print("Hello ", name)
print('a'+'b')
Result: ab
The above code performs string concatenation. No space!
print('a','b')
Result: a b
The above code combines strings for output. By separating strings by comma, print() will output each string separated by a space by default.
According to the print documentation:
print(*objects, sep=' ', end='\n', file=None, flush=False)
Print objects to the text stream file, separated by sep and followed by end.
The default separator between *objects (the arguments) is a space.
For string concatenation, strings act like a list of characters. Adding two lists together just puts the second list after the first list.
print() function definiton is here: docs
print(*objects, sep=' ', end='\n', file=None, flush=False)
Better to explain everything via code
name = "json singh"
# All non-keyword arguments are converted to strings like str() does
# and written to the stream, separated by sep (which is ' ' by default)
# These are separated as two separated inputs joined by `sep`
print("Hello", name)
# Output: Hello json singh
# When `sep` is specified like below
print("Hello", name , sep=',')
# Output: Hello,json singh
# However the function below evaluates the output before printing it
print("Hello" + name)
# Output: Hellojson singh
# Which is similar to:
output = "Hello" + name
print(output)
# Output: Hellojson singh
# Bonus: it'll evaluate the input so the result will be 5
print(2 + 3)
# Output: 5
This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed last year.
for example I entered follwoing string ;
" hello I am Mohsen" ;
now I want to Print on output :
"Mohsen am I hello "
please help me !
Both Corralien's and Tobi208's works, and can be combined to the shorter version;
s = "hello I am Mohsen"
print(' '.join(s.split(' ')[::-1]))
or if you want to input the string in the terminal as a prompt;
s = input()
print(' '.join(s.split(' ')[::-1]))
Split by space, reverse the list, and stitch it back together.
s = " hello I am Mohsen"
words = s.split(' ')
words_reversed = words[::-1]
s_reversed = ' '.join(words_reversed)
print(s_reversed)
This question already has answers here:
Check if string contains only whitespace
(11 answers)
Closed 4 years ago.
I have a variable:
exchange_name = ''
Now I want to perform some operation based on checking if it is equal to an empty string.
So, I do:
if exchange_name == '':
# perform some operation
But how can I generalize the solution so that exchange_name can contain any number of spaces, e.g.:
exchange_name = ' '
or
exchange_name = ' '
Can anybody suggest an approach? Thanks in advance.
exchange_name.strip()==''
strip removes all empty spaces.
Try to use rstrip to remove spaces from begin and end of string.
if mytext.rstrip() == '':
do_it()
This question already has answers here:
Join items of a list with '+' sign in a string
(4 answers)
Closed 6 years ago.
For example if I have code:
print("Give me keywords")
keywords = input()
print("Those are nice " + keywords)
lets say I input:
"Banana cocktail"
Now the question is: Is it possible for me to get it print:
"Those are nice Banana + cocktail".
So the idea is to get + sign every time the user puts space in their input.
Either split and join as suggested in the comments, or replace:
print("Give me keywords")
keywords = input()
print("Those are nice " + keywords.replace(' ', ' + '))
Try this:
print("Those are nice ", ' + '.join(keywords.split()))
There are many options:
keywords.replace(' ', ' + ')
' + '.join(keywords.split())
re.sub('\s+', ' + ', keywords)
You should choose the method depending on that how many spaces do you have between words,
want you replace only spaces or some other things etc.
you can split keywords and join the resultant list with '+'
' + '.join(keywords.split(' '))
This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 6 years ago.
For some reason string.replace(" ", "") is not working.
Is there another way of removing white spaces from a string?
Why is .replace not working in this particular situation?
string = input('enter a string to see if its a palindrome: ')
string.replace(' ', '') # for some reason this is not working
# not sure why program will only work with no spaces
foo = []
bar = []
print(string)
for c in string:
foo.append(c)
bar.append(c)
bar.reverse()
if foo == bar:
print('the sentence you entered is a palindrome')
else:
print('the sentence you entered is not a palindrome')
replace() returns a new string, it doesn't modify the original. Try this instead:
string = string.replace(" ", "")