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

71%
+3 −0
Q&A How can I output / display multiple values, with controlled spacing, without building a string?

I know that I can display a single string in the terminal window like print("example") (or similarly with a string in a variable), and I know how to open text files and write to them. I also know ...

1 answer  ·  posted 1y ago by Karl Knechtel‭  ·  last activity 1y ago by Karl Knechtel‭

Question python format output
#1: Initial revision by user avatar Karl Knechtel‭ · 2023-08-06T05:37:30Z (over 1 year ago)
How can I output / display multiple values, with controlled spacing, without building a string?
I know that I can display a single string in the terminal window like `print("example")` (or similarly with a string in a variable), and I know how to open text files and write to them.

I also know that I can create a "formatted" string, that contains multiple pieces of information, [in a variety of ways](https://software.codidact.com/posts/289251). So if I want to output multiple things, obviously there's the option of creating that string and outputting it.

But sometimes for more complex code, it seems impractical to build a string ahead of time. I experimented a little, and found that:

* if I just call `print` multiple times, each value appears on a separate line.
* if I use the `write` method of an open file multiple times, the values appear consecutively, without any space in between.

A lot of the time, neither of these is what I want. For example, I might want to separate each new item with a space as they're output. More generally, I'd like to be able to control the formatting.

What options do I have for doing this? Are there any "gotchas" I should be aware of?