Concatenate string and int in python 3.4

前端 未结 6 2080
青春惊慌失措
青春惊慌失措 2021-01-06 05:27

I\'m new to Python, so I\'ve been running through my own set of exercises to simply start memorizing basic functions and syntax. I\'m using Pycharm IDE and Python 3.4. I\'ve

相关标签:
6条回答
  • 2021-01-06 05:59

    Please remember (or if you don't know it yet, read up on the subject) that print is a function in Python 3. In Python 2, your first line would concatenate "Type string: " and "123" and then print them. In Python 3, you are calling the print function with one argument, which returns None, and then add "123" to it. That doesn't make any sense.

    The second line doesn't generate an error in Python 2 or 3 (I've tested it with 2.7.7 and 3.2.3). In Python 2, you get

    Concatenate strings and ints 10

    while in Python 3, your script should only print

    Concatenate strings and ints

    This is because again, print is a function, therefore you call it with the argument "Concatenate strings and ints". The , 10 makes your line a tuple of the return value of print, which is None, and 10. Since you don't use that tuple for anything, there is no visible effect.

    0 讨论(0)
  • 2021-01-06 06:01

    i think this is pretty cool way to Concatenate string and int in python

    print (f"Type string: {123}")
    print (f"Concatenate strings and ints {10}")
    
    0 讨论(0)
  • 2021-01-06 06:02

    What's wrong with this?

    print ("Type string: ") + str(123)
    

    print is just a function like anything else. And you're calling that function with one argument, "Type string: ", and then trying to add the result (which will be None) to the string '123'. That isn't going to work. If you wanted to add the two strings together, you have to put them into the same expression, inside the parens:

    print("Type string: " + str(123))
    

    Similarly:

    print ("Concatenate strings and ints "), 10
    

    This calls print with one argument, and then makes a tuple of the None returned by print and the number 10. If you want to pass 10 to the print call, it has to go inside the parens:

    print("Concatenate strings and ints ", 10)
    

    As rednaw's answer points out, using str.format is more flexible, and avoids the possibility of problems like this. It also gives you code that works exactly the same way in both Python 2.6-2.7 and Python 3.x, which is pretty nice even if you aren't trying to write dual-platform/single-codebase code, because it'll be understandable even to people who only know one or the other.

    0 讨论(0)
  • 2021-01-06 06:11

    You can do it like this:

    c = 'Emerson'
    d = 32
    print("My name is %s and I am %d years old." %(c,d))
    

    Result:

    My name is Emerson and I am 32 years old.

    0 讨论(0)
  • 2021-01-06 06:17

    Try format():

    print("Type string: {}".format(123))
    print("Concatenate strings and ints {}".format(10))
    
    0 讨论(0)
  • 2021-01-06 06:24

    In Python 3+, print is a function, so it must be called with its arguments between parentheses. So looking at your example:

    print ("Type string: ") + str(123)
    

    It's actually the same as:

    var = print("Type string: ")
    var + str(123)
    

    Since print returns nothing (in Python, this means None), this is the equivalent of:

    None + str(123)
    

    which evidently will give an error.

    That being said about what you tried to do, what you want do to is very easy: pass the print function what you mean to print, which can be done in various ways:

    print ("Type string: " + str(123))
    
    # Using format method to generate a string with the desired contents
    print ("Type string: {}".format(123)) 
    
    # Using Python3's implicit concatenation of its arguments, does not work the same in Python2:
    print ("Type string:", str(123)) # Notice this will insert a space between the parameters
    
    0 讨论(0)
提交回复
热议问题