Computer Science Canada

Strings

Author:  Sniper4Life [ Wed Apr 15, 2009 6:35 pm ]
Post subject:  Strings

Could someone give me tutorial on the following subjects in pythong: ????

- Slices
- Escape sequences
- len()
- String Methods
? upper
? lower
? find
? replace
? count

Then I would like to know ALL ways you can manipulate a user's input.

ok also...when you have a string like "My foot" they are 2 seperate strings under one function or w/e...and is there any way
to program in python...so that the computer can identify WHERE the space is...when print the words out seperately? so what im basically asking for this question is how do you slice a user's input? like make the output come out SLICED

Author:  Alexmula [ Wed Apr 15, 2009 8:44 pm ]
Post subject:  RE:Strings

look up the string module. and you can split "my foot" with the split function Laughing

Author:  Sniper4Life [ Wed Apr 15, 2009 9:15 pm ]
Post subject:  RE:Strings

yes i know that but what im trying to say is can you do that WITH user input?
like split w/e they type?

Author:  wtd [ Wed Apr 15, 2009 9:58 pm ]
Post subject:  RE:Strings

Can you get what users type into a string?

Author:  Sniper4Life [ Thu Apr 16, 2009 7:19 am ]
Post subject:  RE:Strings

yes they need to write their fulll name so (First name) (Last name)

but every time...the number of characters can be different but it is only 2 words under one raw input every time

but i dont know how to slice that string
cus its user input

Author:  wtd [ Thu Apr 16, 2009 12:45 pm ]
Post subject:  RE:Strings

A string is a string is a string.

Author:  Sniper4Life [ Thu Apr 16, 2009 3:00 pm ]
Post subject:  RE:Strings

sigh just nvr mind...i dont even understand what i want answered Sad
cus i understand all of the things i listed

but i just dont understand...how to manipulate...the user input...

Author:  Tony [ Thu Apr 16, 2009 3:07 pm ]
Post subject:  RE:Strings

re-read this entire thread from the start. Your answer is in here already.

Author:  Dusk Eagle [ Thu Apr 16, 2009 3:23 pm ]
Post subject:  Re: Strings

You're not manipulating anything while the user is entering input. But, once the user has entered input, is there any difference between a string generated like this:
Python:

string = raw_input()

and a string generated like this:
Python:

string = "John Doe"

Author:  Sniper4Life [ Thu Apr 16, 2009 4:41 pm ]
Post subject:  Re: Strings

ok this is basically what its like

code:

string = raw_input ("Please enter your full name")

the user can now enter ANY name
now
code:

what would i enter here...so that W/E they inserted...can be split?


and yes dusk there is
because raw input could be any name
it isnt nessessarily going to be John Doe
so...how can you make it so that you can split a string...without knowing what the string will be? but it has 2 words and 1 space in between it...???

Author:  Dusk Eagle [ Thu Apr 16, 2009 6:02 pm ]
Post subject:  Re: Strings

Python:

namelength = 2 #Number of names we want (In this case, 2, as we just want a first and last name)
num_words = namelength + 1
while num_words > namelength:
        string = raw_input("Enter your name: ")
        string = string.split()
        num_words = len(string)
print "out of loop"


Is this what you're looking for?

Author:  Alexmula [ Thu Apr 16, 2009 6:55 pm ]
Post subject:  RE:Strings

i believe this is what he means
Python:

name = raw_input("Enter name")
name = str(name)
splitName = name.split()
print splitName

Author:  Dusk Eagle [ Thu Apr 16, 2009 7:07 pm ]
Post subject:  Re: RE:Strings

Alexmula @ Thu Apr 16, 2009 7:55 pm wrote:
i believe this is what he means
Python:

name = raw_input("Enter name")
name = str(name)
splitName = name.split()
print splitName


That's basically what I did, except my code checks to make sure the user has only entered two words, and no more. (It doesn't check if the user entered less than two words, but that's easily fixable.) I have a question about your code though. What is name = str(name) supposed to do? I believe raw_input() automatically treats input as a string.

Author:  Alexmula [ Thu Apr 16, 2009 7:13 pm ]
Post subject:  Re: RE:Strings

Dusk Eagle @ Thu Apr 16, 2009 7:07 pm wrote:
Alexmula @ Thu Apr 16, 2009 7:55 pm wrote:
i believe this is what he means
Python:

name = raw_input("Enter name")
name = str(name)
splitName = name.split()
print splitName


That's basically what I did, except my code checks to make sure the user has only entered two words, and no more. (It doesn't check if the user entered less than two words, but that's easily fixable.) I have a question about your code though. What is name = str(name) supposed to do? I believe raw_input() automatically treats input as a string.

if you dont put that then some u's appear beside the names Embarassed i dont even know what those u's represent Laughing Embarassed anyone care to explain?

Author:  Dusk Eagle [ Thu Apr 16, 2009 7:52 pm ]
Post subject:  Re: Strings

That's funny, I don't get that. Are you using CPython?

Author:  Alexmula [ Thu Apr 16, 2009 8:38 pm ]
Post subject:  RE:Strings

i use PyScripter. python version 2.6.

Author:  Sniper4Life [ Thu Apr 16, 2009 9:59 pm ]
Post subject:  Re: RE:Strings

[quote="Alexmula @ Thu Apr 16, 2009 6:13 pm"]
Dusk Eagle @ Thu Apr 16, 2009 7:07 pm wrote:
Alexmula @ Thu Apr 16, 2009 7:55 pm wrote:
i believe this is what he means
Python:

name = raw_input("Enter name")
name = str(name)
splitName = name.split()
print splitName




that....is exactly what i mean
but when you split it...will it split it into 2 names?
???

Author:  Dusk Eagle [ Thu Apr 16, 2009 10:01 pm ]
Post subject:  Re: Strings

It will split into the number of spaces that are present in the string. You will have to do a bit of checking to ensure that there are exactly two words. You'll probably want to look into the len() function.

Author:  Sniper4Life [ Thu Apr 16, 2009 10:05 pm ]
Post subject:  RE:Strings

wow....i am so stupid...i tried thinking so hard i forgot how to do things the simple way...sigh...thanks
i can take it from here Very Happy
i just wanted to know how to split 2 words
thats it

(2 words with user's input )
btw thanks guys for helping out with this
i couldnt find the answer anywhere in google...and ive been stuck on it for a wile
i was thinking but i forgot to just toy around with random methods...thanks


: