How do I print a variable value into a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bloxox4
    New Member
    • Dec 2019
    • 1

    How do I print a variable value into a string

    I'm rather new to programming, I'm trying to create a program that inputs your name and then prints something like print(user_valu e)"Have a good day").
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 655

    #2
    Use the '+' operator to concatenate strings.

    Code:
    val = input()
    print(val + ", have a good day!")

    Comment

    • Ishan Shah
      New Member
      • Jan 2020
      • 47

      #3
      Following will print the string which you want using simple + operator for concatenating string :

      Code:
      name=input('Enter your name : ')
      print(name+' Have a good day')
      You can also use join() function to concatenate the string with a separator. It’s useful when we have a sequence of strings

      If you don’t want a separator, then use join() function with an empty string.

      Using Join() function, the code is following :

      Code:
      print("".join([name]),'Have a good day')

      Comment

      • hussainmujtaba
        New Member
        • Apr 2020
        • 13

        #4
        Here is a oneliner code that you can use
        Code:
        print("Welcome {}, have a nice day!! ".format(input()))
        This way you don't need to save the name anywhere.
        In case you want to save the name , use this code
        Code:
        name=input()
        print("Welcome {}, have a nice day!! ".format(name))

        Comment

        Working...