Hi. I'm new in python and I need some help.

I need to make a program that does this:

You put in a sentence, then the program puts it in a list. Words can be with one or more spaces; if there are more than one, the program should recognize them as only one. The only thing that is printes is the list.

example:

Type something: Today is sunny and warm

...

Right now I'm stuck here:

list = raw_input("Type something: ")
for string in list:
    if string != " ":
        print string

This thing is useless, it only gets all the spaces out and prints everything else (which it shouldn't be printed but I don't know how to do it another way).

Any help would be much appreciated.

PS
sorry for my English

Recommended Answers

All 7 Replies

My suggestion is:

li = raw_input("Type something: ").split()
print li

Do not use "list" as a variable name. List is a builtin type.
The split method of the string does exactly what you except. See documentation for that.

Totaly forgot to write that I mustn't use split.

Here is solution without using split and lists only strings and itertools generators (as this is clearly homework you should show some effort of solving yourself). I bet this is not what your instructor is expecting for solution!

from __future__ import print_function
from itertools import groupby
try:
    input = raw_input
except:
    pass

def isspace(x):
    return x==' '

sometext = input("Type something: ")
print('['+
      ', '.join(repr(''.join(word))
                     for spaces, word in groupby(sometext, isspace)
                     if not spaces)+
      ']')

Thank you for the answer. I figured a different way to do it, with help of your solution.

I'm writing a new program that counts all the capitalized words in a list.

This is how I put all capitalized word in a list

names = []
for x in msg.split():
    if (x[0].isupper()):
        names.append(x)

I want to know how do I count the words and print them this way:

Name1 3
Name2 2
Name3 5
...

Finki: Please do not hijack (even your own) thread to ask a different question. When this first thread was done, you should have marked it 'solved' (at the link on the bottom of the page). Then, when you had another question, you should start a new thread for it.

So: "solve" this thread, please, and post your question in another.

(Try to make your subject line a little better, too. Maybe 'Need to count capitalized words' or some such thing)

Finki: Please do not hijack (even your own) thread to ask a different question. When this first thread was done, you should have marked it 'solved' (at the link on the bottom of the page). Then, when you had another question, you should start a new thread for it.

So: "solve" this thread, please, and post your question in another.

(Try to make your subject line a little better, too. Maybe 'Need to count capitalized words' or some such thing)

Ok, thanks for info ;).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.