0

Can the sum function in python add strings?

8th Sep 2021, 1:36 PM
Conor Walsh
Conor Walsh - avatar
3 odpowiedzi
+ 3
We don't sum strings really, we concatenate them. The sum() function accepts an iterable and expected it to contain items of numerical data. If you want to concatenate all the items of an iterable containing strings, you can use join() method of `str` class. words = ( "sunshine", "daisies", "butter", "mellow", "turn", "this", "smart", "fit", "rat", "yellow" ) print( " ".join( words ) )
8th Sep 2021, 2:21 PM
Ipang
+ 1
Thanks, ipang. Question solved. (love the Harry Potter reference)
8th Sep 2021, 2:23 PM
Conor Walsh
Conor Walsh - avatar
+ 1
Good job Conor Walsh 👍
8th Sep 2021, 2:26 PM
Ipang