Results 1 to 7 of 7

Thread: No Space After Variables in Python (3.2)

  1. #1
    Join Date
    Mar 2011
    Location
    Pizza Hut
    Beans
    1,209
    Distro
    Kubuntu 13.10 Saucy Salamander

    No Space After Variables in Python (3.2)

    I'm writing a script in Python 3.2 and I'm wondering how to insert a variable into a string without there being a space after it. If your not sure what I mean I can give an example.
    Ubuntu user #35115 - Linux user #555707 - My Ubuntu Wik page
    Join the Ubuntu Forums' IRC room (#ubuntuforums on irc.freenode.net)!

  2. #2
    Join Date
    Sep 2005
    Location
    Rural Nevada, USA
    Beans
    306
    Distro
    Ubuntu 15.10 Wily Werewolf

    Re: No Space After Variables in Python (3.2)

    don't you mean something like

    mystring = "first part of text " + str(variable) + "second part of text"

  3. #3
    Join Date
    Sep 2009
    Location
    Canada, Montreal QC
    Beans
    1,809
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: No Space After Variables in Python (3.2)

    You should post a some code. How do you insert the variable into the string? Using concatenation or do you use the format method?
    Using format:
    Code:
     
    "{0} {1} {2}".format(var0, var1, var2)
    I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.
    Freedom is measured in Stallmans.
    Projects: gEcrit

  4. #4
    Join Date
    Mar 2010
    Location
    Norway
    Beans
    674

    Re: No Space After Variables in Python (3.2)

    Example, yes! Don't ask, just give an example in the first post, we can't read your mind but we can read code!

    Python does not insert a space in a string variable. The print function, however, does. Also maybe you're confusing space with EOL, so I'll just give examples for both.

    Get ridd of space and EOL after print:
    Code:
    print("Hello",end=''),
    print("World!")
    # "end=''" removes extra space, the "," removes the EOL character.
    Edit: You don't need the "," in this situation, the "end='' " also removes the EOL character.
    Last edited by Smart Viking; August 31st, 2011 at 10:22 AM.

  5. #5
    Join Date
    Mar 2011
    Location
    Pizza Hut
    Beans
    1,209
    Distro
    Kubuntu 13.10 Saucy Salamander

    Re: No Space After Variables in Python (3.2)

    Example:
    Code:
    name = input("What is your name? ")
    print("Hello, ", name, "!")
    So if you entered "Joe Smith" as your name you would get the output:
    Code:
    Hello,  Joe Smith !
    What I am looking for is this:
    Code:
    Hello, Joe Smith!
    Anyway, someone mentioned concatenation and I tried that. It worked. I forgot that you could use that for variables. So anyway, solved. Thanks people!
    Ubuntu user #35115 - Linux user #555707 - My Ubuntu Wik page
    Join the Ubuntu Forums' IRC room (#ubuntuforums on irc.freenode.net)!

  6. #6
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: No Space After Variables in Python (3.2)

    Quote Originally Posted by dniMretsaM View Post
    Example:
    Code:
    name = input("What is your name? ")
    print("Hello, ", name, "!")
    So if you entered "Joe Smith" as your name you would get the output:
    Code:
    Hello,  Joe Smith !
    What I am looking for is this:
    Code:
    Hello, Joe Smith!
    Anyway, someone mentioned concatenation and I tried that. It worked. I forgot that you could use that for variables. So anyway, solved. Thanks people!
    You can also use the % operator:
    Code:
    name='Joe Smith'
    print 'Hello, %s!' % name
    
    Hello, Joe Smith!

  7. #7
    Join Date
    Mar 2011
    Location
    Pizza Hut
    Beans
    1,209
    Distro
    Kubuntu 13.10 Saucy Salamander

    Re: No Space After Variables in Python (3.2)

    Quote Originally Posted by ofnuts View Post
    You can also use the % operator:
    Code:
    name='Joe Smith'
    print 'Hello, %s!' % name
    
    Hello, Joe Smith!
    Ok, I'll take note of that. Thanks.
    Ubuntu user #35115 - Linux user #555707 - My Ubuntu Wik page
    Join the Ubuntu Forums' IRC room (#ubuntuforums on irc.freenode.net)!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •