Python 3: How do I .join() input() With A String?
Question:
I want to join a users input
to a string
.
def Username():
Name = ("Name:")
Name.join(input("What's your Dicer's name?:n"))
print(Name)
Username()
If I were to input
the name 'Bradley'
for input()
here is the results:
What's your Dicer's name?:
Bradley
Name:
It didn’t print
what I wanted even though I joined the input
with "Name:"
. Here is what I was expecting. Why is it not occurring?
What's your Dicer's name?:
Bradley
Name: Bradley
Answers:
The join
method of string expects a list (iterable) to join using the string itself. For example:
>>> "-".join(["one", "two"])
'one-two'
joined the list ["one", "two"]
with the "-"
string.
I expect you want something like:
" ".join(["Name:", input("Enter name:")])
joining the string "Name:"
with the value returned by input
.
But that’s a bit hard to read. You might be better off with format:
name = input("Enter name: ")
print("Name: {}".format(name))
This isn’t how str.join() works. The method takes a list or sequence of strings, and joins them into one string, with the “parent” string demarcating the listed values. In your case you most likely want to call
print( " ".join([Name, input( "What's your Dicer's name?:n" ) ] ))
This will put a space between both strings in the list.
EDIT: As noted in comments above, this isn’t a mutating method, and instead returns an entirely new string, which you will have to keep under a new or existing variable.
Reference: http://www.tutorialspoint.com/python/string_join.htm
.join
is an overkill for this. You can simply concatenate the user input to Name
using +
:
def Username():
Name = "Name: "
Name += input("What's your Dicer's name?:n")
print(Name)
Why don’t your something like.
ans = input("What's your Dicer's name?:n")
print("Name:"+ans)
It will give you what you want.
It is the best way to solve the problem what you want. Join is not appropriate in this situation.
Cheers,
John
I want to join a users input
to a string
.
def Username():
Name = ("Name:")
Name.join(input("What's your Dicer's name?:n"))
print(Name)
Username()
If I were to input
the name 'Bradley'
for input()
here is the results:
What's your Dicer's name?:
Bradley
Name:
It didn’t print
what I wanted even though I joined the input
with "Name:"
. Here is what I was expecting. Why is it not occurring?
What's your Dicer's name?:
Bradley
Name: Bradley
The join
method of string expects a list (iterable) to join using the string itself. For example:
>>> "-".join(["one", "two"])
'one-two'
joined the list ["one", "two"]
with the "-"
string.
I expect you want something like:
" ".join(["Name:", input("Enter name:")])
joining the string "Name:"
with the value returned by input
.
But that’s a bit hard to read. You might be better off with format:
name = input("Enter name: ")
print("Name: {}".format(name))
This isn’t how str.join() works. The method takes a list or sequence of strings, and joins them into one string, with the “parent” string demarcating the listed values. In your case you most likely want to call
print( " ".join([Name, input( "What's your Dicer's name?:n" ) ] ))
This will put a space between both strings in the list.
EDIT: As noted in comments above, this isn’t a mutating method, and instead returns an entirely new string, which you will have to keep under a new or existing variable.
Reference: http://www.tutorialspoint.com/python/string_join.htm
.join
is an overkill for this. You can simply concatenate the user input to Name
using +
:
def Username():
Name = "Name: "
Name += input("What's your Dicer's name?:n")
print(Name)
Why don’t your something like.
ans = input("What's your Dicer's name?:n")
print("Name:"+ans)
It will give you what you want.
It is the best way to solve the problem what you want. Join is not appropriate in this situation.
Cheers,
John