Computer Science Canada [Python] Help with strings |
Author: | BigBear [ Sat Mar 21, 2009 1:07 pm ] | ||
Post subject: | [Python] Help with strings | ||
I decided to learn python and have been using a few recources to teach syntax and decided to try McKenzie's exercises found Here I am on the second section (2.2) Question 1 and 2 are similiar but question 1 has another part so I will start with question 2. Get a file name from the user and display just the extension.
This should find where there is a . (period) and get a number then go from that number till the end of the input string. print filename [i] should print the character in the postion of the . till the end of the string.[/url] |
Author: | Insectoid [ Sat Mar 21, 2009 1:20 pm ] | ||
Post subject: | RE:[Python] Help with strings | ||
Why not just get the last character of the string and back up 3 spaces?
btw, I'm don't know any python beyond raw_input, so if something's wrong there, whatever. |
Author: | BigBear [ Sat Mar 21, 2009 1:27 pm ] |
Post subject: | RE:[Python] Help with strings |
Yeah thanks that works well there needs to be another bracket before the : But I am sure that is not due to a lack of python knowledge but a typo ![]() But what if the extension is more than 3 characters like .mpeg or something. Also could someone explain what it wrong with my first attempt. |
Author: | Insectoid [ Sat Mar 21, 2009 1:32 pm ] | ||||
Post subject: | RE:[Python] Help with strings | ||||
should be
|
Author: | [Gandalf] [ Sat Mar 21, 2009 6:31 pm ] | ||
Post subject: | RE:[Python] Help with strings | ||
Or, using slicing:
|
Author: | BigBear [ Sat Mar 21, 2009 7:17 pm ] | ||||||||
Post subject: | Re: RE:[Python] Help with strings | ||||||||
insectoid @ Sat Mar 21, 2009 1:32 pm wrote:
should be
Yeah I tried it that way to see the difference in errors and didn't change it back. Gandalf @ Sat Mar 21, 2009 6:31 pm wrote: Or, using slicing:
This gives me a slightly different error slice indices must be integers or None or have an __index__ method As the for loop gives this error range() integer start argument expected, got list. I think I need to use find because for question 1 it has get full name as one raw_input and display it lastname, first name.
Fixed quote. |
Author: | [Gandalf] [ Sun Mar 22, 2009 12:15 am ] |
Post subject: | RE:[Python] Help with strings |
You're putting []s around the fname.find() call, which makes it into a one-element list. Why are you doing that? ![]() |
Author: | Leela [ Sat May 02, 2009 1:28 pm ] | ||
Post subject: | Re: [Python] Help with strings | ||
Can someone advise me whether my solution to this problem is all right?
Great thanks! |
Author: | [Gandalf] [ Sat May 02, 2009 2:53 pm ] | ||
Post subject: | RE:[Python] Help with strings | ||
Yes, it looks right, and it runs appropriately. However, the beauty of high level languages like Python is there's almost always a neater way of doing things.
![]() |
Author: | BigBear [ Sat May 02, 2009 3:32 pm ] |
Post subject: | RE:[Python] Help with strings |
the syntax tag for python should really have a capital ![]() |
Author: | Leela [ Sat May 02, 2009 4:29 pm ] | ||
Post subject: | Re: RE:[Python] Help with strings | ||
[quote="[Gandalf] @ Sat May 02, 2009 2:53 pm"]Yes, it looks right, and it runs appropriately. However, the beauty of high level languages like Python is there's almost always a neater way of doing things.
![]() Thanks! What is rfind? I only know 'find'. |
Author: | BigBear [ Sat May 02, 2009 4:41 pm ] |
Post subject: | Re: [Python] Help with strings |
Quote: rfind(...)
| S.rfind(sub [,start [,end]]) -> int | | Return the highest index in S where substring sub is found, | such that sub is contained within s[start:end]. Optional | arguments start and end are interpreted as in slice notation. | | Return -1 on failure. Type help(str) |
Author: | Leela [ Sat May 02, 2009 10:23 pm ] |
Post subject: | Re: [Python] Help with strings |
BigBear @ Sat May 02, 2009 4:41 pm wrote: Quote: rfind(...)
| S.rfind(sub [,start [,end]]) -> int | | Return the highest index in S where substring sub is found, | such that sub is contained within s[start:end]. Optional | arguments start and end are interpreted as in slice notation. | | Return -1 on failure. Type help(str) Thanks! |