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.

Comments on How can I build a string from smaller pieces?

Post

How can I build a string from smaller pieces?

+4
−1

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®; baked beans are off'.

Simply writing the values in sequence only works for literal strings:

>>> '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)

Using commas to separate the values gives a tuple instead of a single string:

>>> '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!):

>>> '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?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Somewhat meta comment on questions created to be self-answered (6 comments)
Somewhat meta comment on questions created to be self-answered
Derek Elkins‭ wrote over 1 year ago

I think there are better and worse ways of creating question to be self-answered. The best way is to have a question that you were going to ask but answered before you asked. Next best is to closely model the question off of a real question.

Here, you play a character who comes off as having no understanding of the programming language they are using and who has decided to randomly try things instead of doing a modicum of research. The advice the character needs is not how to concatenate strings, but how to find reputable resources for learning the language they are using.

I would recommend against putting on a persona. If you're going to ask such a question, just have it be more upfront, e.g. "Python has many ways of combining strings. What are the differences between them and when should I use each?" Also, if a question isn't being asked and is well covered in many readily accessible resources, maybe there isn't much value in posting a Q&A on it.

Karl Knechtel‭ wrote over 1 year ago

Regarding research and "readily accessible" information, please see this meta question, where I put quite a bit of effort into explaining my own stance, as well as my thoughts on how to grow the community. The short version: really almost nothing is being asked right now, and the best way I can think of to change that is to make this a "readily accessible resource". But more importantly, having things like this written in advance should greatly facilitate future answers on more specific questions, which then have a ready reference for underlying concepts.

I generally agree about putting on a persona. However, I actually was trying to avoid it here - it's just that there's still a need to set up an example to dissect in the answer(s). If you have specific wording suggestions, please feel free to submit an edit.

mr Tsjolder‭ wrote about 1 year ago

I agree with Derek Elkins‭ that this question comes over as too artificial. In the list you linked in the answer to the meta question, you list these top questions. One common aspect of these questions seems to be that they have very short and simple question statements. These questions are popular because people who actually have this question can immediately identify whether this question is relevant for them. These people probably won't know whether they are running Python 3.9 or 3.10 and they might also get confused by what a tuple is, if they understand what types are at all.

Karl Knechtel‭ wrote about 1 year ago

It seems appropriate to show common non-working attempts, because that will help someone who is redirected here after trying one of them. I'll try to simplify the wording, and move the point about versions to a footnote.

mr Tsjolder‭ wrote about 1 year ago

I definitely agree that showing non-working attempts is generally a good thing. I do feel that the non-working attempts should be conceivable from the standpoint of someone who could have this question, however. I just can't imagine that someone would think about tuples in this kind of scenario.

Karl Knechtel‭ wrote about 1 year ago

"I just can't imagine that someone would think about tuples in this kind of scenario." I think you've misunderstood. The word "tuple" here is only used to describe the result of the non-working attempt.