Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
+ is string concatenation and only be applied by strings. Non-string operands like numbers or class instances must be converted to strings using str(). That's all there really is to it, except Pyth...
Answer
#3: Post edited
- `+` is string concatenation and only be applied by strings. Non-string operands like numbers or class instances must be converted to strings using `str()`. That's all there really is to it, except Python has some syntactic sugar that hides this in certain situations.
- When I have a list `u` of *things* (i.e. may or may not be strings) that I want to combine into a string `s`, my go to is:
- ```
- s = "".join(map(str, u))
- ```
This is somewhat advanced Python syntax but it's not that complex. `map` applies `str` to every element of `u`, and `join` glues them all together with an empty string as a delimiter (so that we're not adding anything extra). This is easy to remember, easy to type, and works with everything. You can modify it with various ways as well, such as list comprehensions or passing a lambda instead of `str`.- Syntactic sugars include:
- * `print` takes any object, not just strings, and will automatically convert to string. It can also handle multiple arguments. Therefore `print(8, " cans of spam")` will work, (although `print(8 + " cans of spam")` will not, because the concatenation must evaluate before print).
- * String formatting also auto-converts. Therefore `"{} cans of {}".format(8, "spam")` will work. `f"{8} cans of {meat_name}"` is just syntactic sugar for this. The `%` operator is the same, but it's an ancient Python syntax and I consider it superseded by f-strings.
- * When you put strings next to each other like `"hello" "world"` the `+` is implicit. Because, well, what else could you possibly mean besides concatenation?
- `+` is string concatenation and only be applied by strings. Non-string operands like numbers or class instances must be converted to strings using `str()`. That's all there really is to it, except Python has some syntactic sugar that hides this in certain situations.
- When I have a list `u` of *things* (i.e. may or may not be strings) that I want to combine into a string `s`, my go to is:
- ```
- s = "".join(map(str, u))
- ```
- This is somewhat advanced Python syntax but it's not that complex. `map` applies `str` to every element of `u`, and `join` glues them all together with an empty string as a delimiter (so that we're not adding anything extra). This is easy to remember, easy to type, and works with everything. You can modify it with various ways as well, such as passing list/sequence comprehensions instead of `u` or a lambda instead of `str`.
- Syntactic sugars include:
- * `print` takes any object, not just strings, and will automatically convert to string. It can also handle multiple arguments. Therefore `print(8, " cans of spam")` will work, (although `print(8 + " cans of spam")` will not, because the concatenation must evaluate before print).
- * String formatting also auto-converts. Therefore `"{} cans of {}".format(8, "spam")` will work. `f"{8} cans of {meat_name}"` is just syntactic sugar for this. The `%` operator is the same, but it's an ancient Python syntax and I consider it superseded by f-strings.
- * When you put strings next to each other like `"hello" "world"` the `+` is implicit. Because, well, what else could you possibly mean besides concatenation?
#2: Post edited
- `+` is string concatenation and only be applied by strings. Non-string operands like numbers or class instances must be converted to strings using `str()`. That's all there really is to it, except Python has some syntactic sugar that hides this in certain situations.
- When I have a list `u` of *things* (i.e. may or may not be strings) that I want to combine into a string `s`, my go to is:
- ```
- s = "".join(map(str, u))
- ```
This is somewhat advanced Python syntax but it's not that complex. `map` applies `str` to every element of `u`, and `join` glues them all together with an empty string as a delimiter (so that we're not adding anything extra). This is easy to remember, easy to type, and works with everything. You can modify it with various ways as well, such as list comprehensions.- Syntactic sugars include:
- * `print` takes any object, not just strings, and will automatically convert to string. It can also handle multiple arguments. Therefore `print(8, " cans of spam")` will work, (although `print(8 + " cans of spam")` will not, because the concatenation must evaluate before print).
- * String formatting also auto-converts. Therefore `"{} cans of {}".format(8, "spam")` will work. `f"{8} cans of {meat_name}"` is just syntactic sugar for this. The `%` operator is the same, but it's an ancient Python syntax and I consider it superseded by f-strings.
- * When you put strings next to each other like `"hello" "world"` the `+` is implicit. Because, well, what else could you possibly mean besides concatenation?
- `+` is string concatenation and only be applied by strings. Non-string operands like numbers or class instances must be converted to strings using `str()`. That's all there really is to it, except Python has some syntactic sugar that hides this in certain situations.
- When I have a list `u` of *things* (i.e. may or may not be strings) that I want to combine into a string `s`, my go to is:
- ```
- s = "".join(map(str, u))
- ```
- This is somewhat advanced Python syntax but it's not that complex. `map` applies `str` to every element of `u`, and `join` glues them all together with an empty string as a delimiter (so that we're not adding anything extra). This is easy to remember, easy to type, and works with everything. You can modify it with various ways as well, such as list comprehensions or passing a lambda instead of `str`.
- Syntactic sugars include:
- * `print` takes any object, not just strings, and will automatically convert to string. It can also handle multiple arguments. Therefore `print(8, " cans of spam")` will work, (although `print(8 + " cans of spam")` will not, because the concatenation must evaluate before print).
- * String formatting also auto-converts. Therefore `"{} cans of {}".format(8, "spam")` will work. `f"{8} cans of {meat_name}"` is just syntactic sugar for this. The `%` operator is the same, but it's an ancient Python syntax and I consider it superseded by f-strings.
- * When you put strings next to each other like `"hello" "world"` the `+` is implicit. Because, well, what else could you possibly mean besides concatenation?
#1: Initial revision
`+` is string concatenation and only be applied by strings. Non-string operands like numbers or class instances must be converted to strings using `str()`. That's all there really is to it, except Python has some syntactic sugar that hides this in certain situations. When I have a list `u` of *things* (i.e. may or may not be strings) that I want to combine into a string `s`, my go to is: ``` s = "".join(map(str, u)) ``` This is somewhat advanced Python syntax but it's not that complex. `map` applies `str` to every element of `u`, and `join` glues them all together with an empty string as a delimiter (so that we're not adding anything extra). This is easy to remember, easy to type, and works with everything. You can modify it with various ways as well, such as list comprehensions. Syntactic sugars include: * `print` takes any object, not just strings, and will automatically convert to string. It can also handle multiple arguments. Therefore `print(8, " cans of spam")` will work, (although `print(8 + " cans of spam")` will not, because the concatenation must evaluate before print). * String formatting also auto-converts. Therefore `"{} cans of {}".format(8, "spam")` will work. `f"{8} cans of {meat_name}"` is just syntactic sugar for this. The `%` operator is the same, but it's an ancient Python syntax and I consider it superseded by f-strings. * When you put strings next to each other like `"hello" "world"` the `+` is implicit. Because, well, what else could you possibly mean besides concatenation?