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
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...
#3: Post edited
- 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
- 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
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?