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

Post History

60%
+1 −0
Q&A Tips for golfing in Python

Replace for loops with string multiplication Let's say we have: for i in range(6):print(end="#") Try it online! This basically outputs # 6 times, which is self-explanatory in the code itsel...

posted 3y ago by General Sebast1an‭

Answer
#1: Initial revision by user avatar General Sebast1an‭ · 2021-09-07T03:37:43Z (about 3 years ago)
# Replace `for` loops with string multiplication

Let's say we have:
<!-- language-all: lang-python -->

    for i in range(6):print(end="#")

[Try it online!][TIO-kt9iruha]

[TIO-kt9iruha]: https://tio.run/##K6gsycjPM/7/Py2/SCFTITNPoSgxLz1Vw0zTqqAoM69EIzUvxVZJWUnz/38A "Python 3 – Try It Online"

This basically outputs `#` 6 times, which is self-explanatory in the code itself. Perhaps we can shorten it with string multiplication:
<!-- language-all: lang-python -->

    print(end="#"*6)

[Try it online!][TIO-kt9iuhaq]

[TIO-kt9iuhaq]: https://tio.run/##K6gsycjPM/7/v6AoM69EIzUvxVZJWUnLTPP/fwA "Python 3 – Try It Online"

Same result, lesser bytes.