user1411893
user1411893

Reputation: 628

Printing x amounts of characters in terminal or python?

If i need x amount of characters to test my program, is there a way of generating them quickly with terminal in linux? or in python? for example I want to test if my program breaks when I enter 80 characters. But instead of writing 80 characters I want to be able to just generate 80 characters in terminal and then copy paste it to my program (or pipeline it etc). I tried doing:

>>> for item in range (1,80):
...     print "x",

It works but it prints x with spaces inbetween which would be more than 80 characters

Upvotes: 0

Views: 793

Answers (5)

Ryan Haining
Ryan Haining

Reputation: 36882

you can use the * operator on a character to repeat it 80 times. so just make a small file called "out.py" containing the line

print 'x' * 80

then write

$ python out.py | python prgm.py

in the terminal where prgm.py is the name of your python program

Upvotes: 0

John La Rooy
John La Rooy

Reputation: 304375

Does it have to be 'x's? For a random string you can use this from the shell

$ dd if=/dev/urandom count=80 bs=1 

Upvotes: 2

Julien Oster
Julien Oster

Reputation: 2322

On the shell, you can abuse seq:

% seq -f foo -s '' 10
foofoofoofoofoofoofoofoofoofoo

If you want your strings to be separated by newlines, using yes is easiest:

% yes
y
y
y
...
% yes foo
foo
foo
foo
...
% yes twice | head -2
twice
twice

Upvotes: 3

PasteBT
PasteBT

Reputation: 2198

try this:

print "x" * 80

or

print "".join([str(x) for x in xrange(80)])

this will generate long list

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 994021

In Python, the multiplication operator * can apply to strings.

print "x" * 80

Upvotes: 4

Related Questions