0

Let's say I want to add these strings together: "I" "use" "Python"

Firstly, I could concatenate them using the + sign like this:

print("I " + "use " + "Python")

But that is bulky and wastes a lot of time, especially when you need to combine a lot of strings. Also, note that I embedded the spaces into the string, whereas I usually have to use variables, meaning that I have to add spaces separately, which is an even bigger pain.

Secondly, there is a way to add with commas:

print("I", "use", "Python")

This is what I want, however this has a caveat. It doesn't seem to work in return functions.

def function():
  return "I", "use", "Python"
print(function())

This returns a tuple, instead of the string I want, which means that sigh, I have to use the boring plus sign concatenation. This sucks up tons of time because I need to continuously add spaces.

So back to the main question, how do I use this method in a return statement?

Z9.
  • 238
  • 2
  • 15
  • 1
    Mind explaining how `"I" + "use" + "Python"` is worse than `"I", "use", "Python"`? Seems about the same number of characters to me – Chase May 04 '21 at 08:33
  • 1
    What makes you think concatenating strings with `+` wastes time? And why do you think `,` doesn't waste time? (`,` doesn't concatenate strings at all, by the way.) – user2357112 May 04 '21 at 08:33
  • @Chase In a print statement you need to add spaces manually if you use `+` signs. – Asocia May 04 '21 at 08:34
  • 1
    Note that `,` does not "concatenate" strings. `print` is a function, guess what `,` does in a function - that's right, it separates arguments. That's all it does there. It just so happens that the `print` function concatenates all its arguments with a space. Same way the answer in the linked question does. – Chase May 04 '21 at 08:34
  • As others already said string concatenation doesn't take any longer time in python. However in pypy3 it does take almost O(n) time, so better watch it there pypy users! – edusanketdk May 04 '21 at 08:45
  • Did you mean: `print(*function())`? – quamrana May 04 '21 at 08:45
  • Hi guys, what I meant was that it didn't work when I tried to do it in a return statement, as you can see in the last one. Also, in return statements, you need to add a space like every other word, which takes forever when you have a lot of words, and it seems like everyone is glossing over the "in a function part", which I will reword. Sorry if the question was poorly worded. – Z9. May 04 '21 at 13:16
  • Also, i didn't mean how long it takes to run, I meant how long it takes to type. – Z9. May 04 '21 at 13:19

2 Answers2

1

You can use f-strings if you are on Python3.6+ or format your strings with .format.

def foo(name, age):
    return f'{name} is {age} years old.'

def bar(name, age):
    return '{} is {} years old'.format(name, age)

>>> foo('Ben', 25)
'Ben is 25 years old.'
>>> bar('Tom', 20)
'Tom is 20 years old'

If you have list of strings that you need to join with some character, use .join

>>> my_list = ['I', 'love', 'Python']
>>> ' '.join(my_list)
'I love Python'
Asocia
  • 5,935
  • 2
  • 21
  • 46
-1
s1 = 'I'
s2 = 'use'
s3 = 'python'

def function(*strings):
    x = ''
    for s in strings:
        x += s + ' '

    return x

print(function(s1,s2,s3))
Wrench
  • 490
  • 4
  • 13
  • 2
    This sticks an extra space on the end of the string, and aside from that bug, is a slow and unnecessary reimplementation of `''.join`. – user2357112 May 04 '21 at 08:35
  • 1
    While code-only answers might answer the question, you could significantly improve the quality of your answer by providing context for your code, a reason for why this code works, and some references to documentation for further reading. From [answer]: _"Brevity is acceptable, but fuller explanations are better."_ – Pranav Hosangadi May 04 '21 at 21:28