Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

66%
+2 −0
Q&A How can I build a string from smaller pieces?

+ 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...

posted 1y ago by matthewsnyder‭  ·  edited 1y ago by matthewsnyder‭

Answer
#3: Post edited by user avatar matthewsnyder‭ · 2023-08-05T18:52:50Z (over 1 year ago)
  • `+` 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 by user avatar matthewsnyder‭ · 2023-08-05T18:52:20Z (over 1 year ago)
  • `+` 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 by user avatar matthewsnyder‭ · 2023-08-05T18:50:06Z (over 1 year ago)
`+` 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?