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%
+4 −1
Q&A How can I build a string from smaller pieces?

Suppose I have some variables like: >>> count = 8 >>> status = 'off' I want to combine them with some hard-coded text, to get a single string like 'I have 8 cans of Spam®; b...

2 answers  ·  posted 1y ago by Karl Knechtel‭  ·  last activity 1y ago by Karl Knechtel‭

#3: Post edited by user avatar Karl Knechtel‭ · 2023-08-08T01:47:25Z (over 1 year ago)
fix error message as shown in contemporary versions; add note about how it looks in older versions
  • Suppose I have some variables like:
  • ```python
  • >>> count = 8
  • >>> status = 'off'
  • ```
  • I want to combine them with some hard-coded text, to get a single string like
  • `'I have 8 cans of Spam®; baked beans are off'`.
  • Simply writing the values in sequence only works for literal strings:
  • ```python
  • >>> 'one' 'two'
  • 'onetwo'
  • >>> 'I have' count 'cans of Spam®; baked beans are' status
  • File "<stdin>", line 1
  • 'I have' count 'cans of Spam®; baked beans are' status
  • ^
  • SyntaxError: invalid syntax
  • ```
  • Using commas to separate the values gives a tuple instead of a single string:
  • ```python
  • >>> 'I have', count, 'cans of Spam®; baked beans are', status
  • ('I have', 8, '; baked beans are', 'off')
  • ```
  • "Adding" the strings doesn't work either (I know this is not like mathematics, but it works in some other languages!):
  • ```python
  • >>> 'I have' + count + 'cans of Spam®; baked beans are' + status
  • Traceback (most recent call last):
  • File "<stdin>", line 1, in <module>
  • TypeError: can only concatenate str (not "int") to str
  • ```
  • How am I meant to do it?
  • Suppose I have some variables like:
  • ```python
  • >>> count = 8
  • >>> status = 'off'
  • ```
  • I want to combine them with some hard-coded text, to get a single string like
  • `'I have 8 cans of Spam®; baked beans are off'`.
  • Simply writing the values in sequence only works for literal strings:
  • ```python
  • >>> 'one' 'two'
  • 'onetwo'
  • >>> 'I have' count 'cans of Spam®; baked beans are' status
  • File "<stdin>", line 1
  • 'I have' count 'cans of Spam®; baked beans are' status
  • ^^^^^
  • SyntaxError: invalid syntax
  • ```
  • (Python 3.9 and below will only highlight the "c" of `count`; this improvement was [added in 3.10](https://docs.python.org/3/whatsnew/3.10.html#better-error-messages))
  • Using commas to separate the values gives a tuple instead of a single string:
  • ```python
  • >>> 'I have', count, 'cans of Spam®; baked beans are', status
  • ('I have', 8, '; baked beans are', 'off')
  • ```
  • "Adding" the strings doesn't work either (I know this is not like mathematics, but it works in some other languages!):
  • ```python
  • >>> 'I have' + count + 'cans of Spam®; baked beans are' + status
  • Traceback (most recent call last):
  • File "<stdin>", line 1, in <module>
  • TypeError: can only concatenate str (not "int") to str
  • ```
  • How am I meant to do it?
#2: Post edited by user avatar Karl Knechtel‭ · 2023-08-08T01:44:13Z (over 1 year ago)
edit example to match the changes in my answer (to shorten it and avoid line-wraps)
  • Suppose I have some variables like:
  • ```python
  • >>> count = 8
  • >>> status = 'off'
  • ```
  • I want to combine them with some hard-coded text, to get a single string like
  • `'You have 8 cans of Spam® and the baked beans are off'`.
  • Simply writing the values in sequence only works for literal strings:
  • ```python
  • >>> 'one' 'two'
  • 'onetwo'
  • >>> 'You have' count 'cans of Spam® and the baked beans are' status
  • File "<stdin>", line 1
  • 'You have' count 'cans of Spam® and the baked beans are' status
  • ^
  • SyntaxError: invalid syntax
  • ```
  • Using commas to separate the values gives a tuple instead of a single string:
  • ```python
  • >>> 'You have', count, 'cans of Spam® and the baked beans are', status
  • ('You have', 8, 'and the baked beans are', 'off')
  • ```
  • "Adding" the strings doesn't work either (I know this is not like mathematics, but it works in some other languages!):
  • ```python
  • >>> 'You have' + count + 'cans of Spam® and the baked beans are' + status
  • Traceback (most recent call last):
  • File "<stdin>", line 1, in <module>
  • TypeError: can only concatenate str (not "int") to str
  • ```
  • How am I meant to do it?
  • Suppose I have some variables like:
  • ```python
  • >>> count = 8
  • >>> status = 'off'
  • ```
  • I want to combine them with some hard-coded text, to get a single string like
  • `'I have 8 cans of Spam®; baked beans are off'`.
  • Simply writing the values in sequence only works for literal strings:
  • ```python
  • >>> 'one' 'two'
  • 'onetwo'
  • >>> 'I have' count 'cans of Spam®; baked beans are' status
  • File "<stdin>", line 1
  • 'I have' count 'cans of Spam®; baked beans are' status
  • ^
  • SyntaxError: invalid syntax
  • ```
  • Using commas to separate the values gives a tuple instead of a single string:
  • ```python
  • >>> 'I have', count, 'cans of Spam®; baked beans are', status
  • ('I have', 8, '; baked beans are', 'off')
  • ```
  • "Adding" the strings doesn't work either (I know this is not like mathematics, but it works in some other languages!):
  • ```python
  • >>> 'I have' + count + 'cans of Spam®; baked beans are' + status
  • Traceback (most recent call last):
  • File "<stdin>", line 1, in <module>
  • TypeError: can only concatenate str (not "int") to str
  • ```
  • How am I meant to do it?
#1: Initial revision by user avatar Karl Knechtel‭ · 2023-08-05T16:33:30Z (over 1 year ago)
How can I build a string from smaller pieces?
Suppose I have some variables like:
```python
>>> count = 8
>>> status = 'off'
```
I want to combine them with some hard-coded text, to get a single string like

`'You have 8 cans of Spam® and the baked beans are off'`.

Simply writing the values in sequence only works for literal strings:
```python
>>> 'one' 'two'
'onetwo'
>>> 'You have' count 'cans of Spam® and the baked beans are' status
  File "<stdin>", line 1
    'You have' count 'cans of Spam® and the baked beans are' status
               ^
SyntaxError: invalid syntax
```
Using commas to separate the values gives a tuple instead of a single string:
```python
>>> 'You have', count, 'cans of Spam® and the baked beans are', status
('You have', 8, 'and the baked beans are', 'off')
```
"Adding" the strings doesn't work either (I know this is not like mathematics, but it works in some other languages!):
```python
>>> 'You have' + count + 'cans of Spam® and the baked beans are' + status
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
```
How am I meant to do it?