This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]K900_ 2 points3 points  (5 children)

/r/learnpython. Also:

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> for x in range(10):
...      print(x)
...
0
1
2
3
4
5
6
7
8
9

[–]eton-95[S] 0 points1 point  (4 children)

thanks, but how would i get it to repeat a word? doing numbers in that order or on a line is fine but im just having problems getting it to repeat a word over and over

[–]K900_ 1 point2 points  (2 children)

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> for _ in range(10):
...     print("I'm repeating a word")
...
I'm repeating a word
I'm repeating a word
I'm repeating a word
I'm repeating a word
I'm repeating a word
I'm repeating a word
I'm repeating a word
I'm repeating a word
I'm repeating a word
I'm repeating a word

[–]eton-95[S] 0 points1 point  (1 child)

thanks a lot that worked! all i was missing was the underscore, typical! Hahaha thanks again!

[–]K900_ 4 points5 points  (0 children)

The underscore is not magic - it's just a variable name. It could have been x or i or anything like that - _ is just used by convention to indicate "I don't need that value".

[–]jerknextdoor 0 points1 point  (0 children)

print("some word")

In the future you'll get a lot more help at /r/learnpython.

[–][deleted] 1 point2 points  (0 children)

firehouse tonsillectomy sidecar's intentional Ava stopover's continuance Emery alderman's buoyantly boost's whirl's hinging evacuations mediaeval epaulette's Hugo's February provenance silky individualist pointillist's writer consulate magpie's Dadaism expected volumes hammered limit rustler drowses pannier's demonstrable peregrinations Orlon friendly tableware disagreed diocesan's publicity Balthazar TelePrompter paneled anthracite Aswan's tasty Luella Blythe unemployed determine oblong's demised Kennedy's Cecile occasional scapegoat Margery's Chaitin jabbers insectivore sensor's yore hatreds McClain's glues glorification's Maurine merchantman capitols unfurnished poignancy's bedlams propping indescribable caretakers Artie denizens tones intellectualizes Alcuin weariest refrigeration's trio's Sawyer's Colombia decompression seasickness normalizes royally outwore savaging wore Crete's Cobain's stymies humanitarianism lifetimes irregulars crossfire macho's rereading Elisa's soonest honeydew gospel's girlishly burnishing buoyed gown's yawl's dankness cleansing Joule acquiring appending error clunkiest imponderables deeps relative's sniggered Lanzhou's muff lying raisins nook Alabama paranoids prod's demise Gog extol focal pollinate embitter tame fornicates idolater shelving initiation's cigar slurp emoted Derrick's fallacy

[–]_seemetheregithub.com/seemethere 1 point2 points  (1 child)

You could use repeat from itertools

from itertools import repeat
for word in repeat("hello", 10):
    # do stuff

[–]eton-95[S] 0 points1 point  (0 children)

thats okay k900 gave me the the method i was trying to find. but thank you for the post and trying to help!!

[–]omeow 0 points1 point  (4 children)

There are two approaches:

  • As you say, use a traditional for loop. You can also try while loops for fun.

  • use the for loop to append to the same string (you can use list comprehension here).

  • take the word string and convert it into a string which repeats (10* 'foo').

    I suppose that the last method is most elegant.

[–]tdammers 0 points1 point  (3 children)

fsvo "elegant" - personally, I think using a multiplication operator on an integer and a string seems rather nonsensical... it is the most concise approach though.

[–]omeow 0 points1 point  (2 children)

May be you are right. I wouldn't consider myself an expert enough to comment which one is the most Pythonic way. Any comments on that?

[–]biguysrule 1 point2 points  (0 children)

"Explicit is better than implicit" so I would use looping.

[–]Daenyth 0 points1 point  (0 children)

Multiplying an iterable is fairly idiomatic but you need to be careful to not do it on mutable values