0

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
philipxy
  • 14,867
  • 6
  • 39
  • 83
  • doing `Name.join(...)` creates a new string that you do nothing with, if you want to change the `Name` variable to the result of `Name.join(...)` then do `Name = Name.join(input("..."))` but I imagine you will be surprised with the result. – Tadhg McDonald-Jensen Jun 09 '16 at 00:54
  • This isn't really what `join` is for, try running `help(str.join)`, it joins a bunch of strings together with `Name` as the seperator, in this case it does each letter of `Bradley` seperated by `"Name:"` so the result will be: `'BName:rName:aName:dName:lName:eName:y'` instead I think you want `Name += input(...)` or just `name = input() ; print("Name:",name)` – Tadhg McDonald-Jensen Jun 09 '16 at 00:57
  • see http://stackoverflow.com/questions/26943256/python-string-replace-not-working – Tadhg McDonald-Jensen Jun 09 '16 at 00:58
  • @TadhgMcDonald-Jensen That stack question was hard to understand. I also stuck with the `Name += input(...) ` one but thanks for your clarification – BradTheBrutalitist Jun 09 '16 at 01:08
  • Does this answer your question? [Why doesn't calling a string method do anything unless its output is assigned?](https://stackoverflow.com/questions/9189172/why-doesnt-calling-a-string-method-do-anything-unless-its-output-is-assigned) – Karl Knechtel Aug 06 '22 at 01:55
  • @KarlKnechtel I will be quite honest with you. I haven't used that account or python in a long time. – Bradyo Aug 08 '22 at 19:35
  • Ah. I am just trying to get the question closed, because I am pretty confident it was a duplicate. I am allowed to close questions unilaterally if they were originally tagged `python`, but this question was originally tagged with `python-3.x` and *not* with `python`, so I have to wait for assistance. – Karl Knechtel Aug 08 '22 at 23:59

4 Answers4

0

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))
Carl Groner
  • 4,149
  • 1
  • 19
  • 20
0

.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)
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
0

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

MooreJohn90
  • 164
  • 1
  • 2
  • 11
-1

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

Jeremy
  • 104
  • 3
  • 12