Wei dong
Wei dong

Reputation: 69

Python: How to print text inputed by user?

My code is as follows:

#!/usr/bin/python
#Filename:2_7.py
text=raw_input("Enter string:")
for ?? in range(??)
    print 

For example, if the user enters test text, I want test text to be printed on the screen by this code.

What should I write instead of ?? to achieve this purpose?

Upvotes: 1

Views: 14216

Answers (3)

Kimtho6
Kimtho6

Reputation: 6184

Maybe you want something like this?

var = raw_input("Enter something: ")
print "you entered ", var

Or in Python 3.x

var = input("Enter something: ")
print ("you entered ", var)

Upvotes: 4

paxdiablo
paxdiablo

Reputation: 881293

Well, for a simplistic case, you could try:

for word in text.split(" "):
    print word

For a more complex case where you want to use a regular expression for a splitter:

for word in re.split("\W+", text):
    if word != "":
        print word

The earlier example will output "Hello, my name is Pax." as:

Hello,
my
name
is
Pax.

while the latter will more correctly give you:

Hello
my
name
is
Pax

(and you can improve the regex if you have more edge cases).

Upvotes: 0

mikej
mikej

Reputation: 66263

Do you want to split the string into separate words and print each one?

e.g.

for word in text.split():
    print word

in action:

Enter string: here are some words
here
are
some
words

Upvotes: 1

Related Questions