Friday, May 1, 2020

Strings in Python - Part 2


String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''. End of lines are automatically included in the string, but it’s possible to prevent this by adding a \ at the end of the line. Produces the following output (note that the initial newline is not included).





Strings can be concatenated (glued together) with the + operator, and repeated with *.


Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.



This only works with two literals though, not with variables or expressions.




If you want to concatenate variables or a variable and a literal, use +.




This feature is particularly useful when you want to break long strings.



No comments:

Post a Comment