Moving around in a string

R

Ryan Spencer

Hello All,

Once again I'm back a with a question, ha ha.

Well, I've gotten to an exercises where it wants me to write a translation
program that takes input from the user and translates it into either pig
Latin or something called "ubby dubby". It describes the basic rules and
all, and I somewhat understand in my mind on how to go about doing it, but...

It requires me to, for the pig Latin part, remove the first letter and
move it to the end of a string (and then add ay to the end.)

How should I go about "moving" to the end of the string?

Just for clarification I want to do something like...

Please enter a word: Orange

rangeOay

(taking the O and bringing it to end and then adding 'ay' to the end)

Thanks as always,

~Ryan
 
T

Troels Therkelsen

Ryan Spencer wrote: said:
Well, I've gotten to an exercises where it wants me to write a translation
program that takes input from the user and translates it into either pig
Latin or something called "ubby dubby". It describes the basic rules and
all, and I somewhat understand in my mind on how to go about doing it, but...

It requires me to, for the pig Latin part, remove the first letter and
move it to the end of a string (and then add ay to the end.)

How should I go about "moving" to the end of the string?

Just for clarification I want to do something like...

Please enter a word: Orange

rangeOay

(taking the O and bringing it to end and then adding 'ay' to the end)

This sounds suspiciously like homework, but...
foo = "Orange"
bar = foo[1:] + foo[0] + 'ay'
bar
'rangeOay'

In other words, you can index strings the same way as any other sequence,
[0] is the first element of the sequence, [1] the second, [-1] is the last,
and so forth.

See also the standard documentation on sequences:

http://www.python.org/doc/current/lib/typesseq.html


Regards,

Troels Therkelsen
 
R

Ryan Spencer

This sounds suspiciously like homework, but...
foo = "Orange"
bar = foo[1:] + foo[0] + 'ay'
bar
'rangeOay'

In other words, you can index strings the same way as any other sequence,
[0] is the first element of the sequence, [1] the second, [-1] is the last,
and so forth.

See also the standard documentation on sequences:

http://www.python.org/doc/current/lib/typesseq.html


Regards,

Troels Therkelsen

Hey Troels,

Thanks for the advice, Pulled it off by.

[start code]

def pig_latin(word):

space = word[0]

result = word[1:]+space+"ay"
print result

[end code]

This all leads me to another problem. In my recent attempts I've
been trying to learn how to parse sentences and such down more and more
(by amt. of characters, words, lines, etc.) If you don't mind another
question, how do I go about say, parsing for words in a string?

I would logically conclude to somehow find a way to declare that
everything with a space between it (or for every space) add one to a
'word_count' variable maybe, but, the question for that would be how do I
get the system to search the string for a certain character, especially if
it is a space?

Thanks a ton,

~Ryan
 
M

Mark Engle

Ryan said:
This sounds suspiciously like homework, but...
foo = "Orange"
bar = foo[1:] + foo[0] + 'ay'
bar
'rangeOay'

In other words, you can index strings the same way as any other
sequence, [0] is the first element of the sequence, [1] the second,
[-1] is the last, and so forth.

See also the standard documentation on sequences:

http://www.python.org/doc/current/lib/typesseq.html


Regards,

Troels Therkelsen

Hey Troels,

Thanks for the advice, Pulled it off by.

[start code]

def pig_latin(word):

space = word[0]

result = word[1:]+space+"ay"
print result

[end code]

This all leads me to another problem. In my recent attempts I've
been trying to learn how to parse sentences and such down more and
more (by amt. of characters, words, lines, etc.) If you don't mind
another question, how do I go about say, parsing for words in a
string?

I would logically conclude to somehow find a way to declare that
everything with a space between it (or for every space) add one to a
'word_count' variable maybe, but, the question for that would be how
do I get the system to search the string for a certain character,
especially if it is a space?

Thanks a ton,

~Ryan

I'm also new to Python, but I think I can point you in the right
direction. Look into split() in library documentation.

http://www.python.org/doc/current/lib/module-string.html

The library documentation is a _very_ good reference. Read that
whole page and you will find bunches of useful information. I
found it to be very clear.

Mark
 
P

Peter Otten

Mark said:
I'm also new to Python, but I think I can point you in the right
direction. Look into split() in library documentation.

http://www.python.org/doc/current/lib/module-string.html

One caveat (only for the string module), quoting from the above document,
but easily overlooked:

"Many of the functions provided by this module are also defined as methods
of string and Unicode objects; see ``String Methods'' (section 2.2.6) for
more information on those."

So whenever you're tempted to write string.somefunc(s, moreargs), rather use
s.somefunc(moreargs).

Peter
 
M

Mark Engle

Peter said:
One caveat (only for the string module), quoting from the above
document, but easily overlooked:

"Many of the functions provided by this module are also defined as
methods of string and Unicode objects; see ``String Methods''
(section 2.2.6) for more information on those."

So whenever you're tempted to write string.somefunc(s, moreargs),
rather use s.somefunc(moreargs).

Peter

Thank you. Time to do some more reading.

Mark
 
R

Ryan Spencer

Thank you. Time to do some more reading.

Mark

Heya Mark,

Thanks for the split function. It did the trick of splitting up the words
to the appropriate white space.

words_within = sentence.split()

When printed, it lists all the words separated by white
space.

I'll be back after some more research however ;)

~Ryan
 
R

Ryan Spencer

Thank you. Time to do some more reading.

Mark

Hello Everyone,

I pulled it off. Thank you for the direction of the split function, Mark.

[start code]

words_within = sentence.split()

word_count = 0

for i in words_within:
word_count = word_count+1

print "There are", word_count, "words."

[end code]

Haha, Now that I've got my parsing fun over with, I can head back to my
translation. All I need to figure out now is how to apply the code

[start code]

pig_latin = word[1:]+word[0]+"ay"

[end code]

to each word in the string. Any advice on what I should do would be
appreciated.

Thanks,

~Ryan
 
C

Cousin Stanley

| words_within = sentence.split()
|
| word_count = 0
|
| for i in words_within:
| word_count = word_count+1
|
| print "There are", word_count, "words."

Ryan ....

You might also find the len() builtin function useful
to provide the length of a list or string ....

sentence = 'yo ho ho and a bottle of rum'
words = sentence.split()
word_count = len( words )

print word_count
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top