Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Python] Help with strings
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
BigBear




PostPosted: 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.

Python:
filename = raw_input ("Enter a file name: ")
num = [filename.find (".",0, len(filename))]
for i in range (len(filename),num):
        print filename[i]


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]
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: 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?

Python:

for i in range (len (filename)-3, len (filename):
    print filename[i]


btw, I'm don't know any python beyond raw_input, so if something's wrong there, whatever.
BigBear




PostPosted: 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 Smile

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.
Insectoid




PostPosted: Sat Mar 21, 2009 1:32 pm   Post subject: RE:[Python] Help with strings

code:

for i in range (len(filename),num):

should be
code:

for i in range (num, len(filename)):
[Gandalf]




PostPosted: Sat Mar 21, 2009 6:31 pm   Post subject: RE:[Python] Help with strings

Or, using slicing:
code:
print filename[num:]
BigBear




PostPosted: Sat Mar 21, 2009 7:17 pm   Post subject: Re: RE:[Python] Help with strings

insectoid @ Sat Mar 21, 2009 1:32 pm wrote:
code:

for i in range (len(filename),num):

should be
code:

for i in range (num, len(filename)):


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:
code:
print filename[num:]


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.

Python:
fname = raw_input("Enter your full name.")
num = [fname.find(" ",0,len(fname))]
for i in range (num,len(fname)):
      print fname [i],
print "",
for j in range (0,num):
    print fname[j],


Fixed quote.
[Gandalf]




PostPosted: 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? Smile
Leela




PostPosted: 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?
Python:
filename = raw_input ('Enter your file: ')
extention = []
index = -1
while filename[index] != ".":
                extention.append(filename[index])
                index = index-1
extention.reverse()
ext = ''.join(extention)
print 'Your extention is', ext

Great thanks!
Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: 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.
Python:
filename = raw_input('Enter your file: ')
ext = filename[filename.rfind(".") + 1:]
print 'Your extention is', ext

Smile
BigBear




PostPosted: Sat May 02, 2009 3:32 pm   Post subject: RE:[Python] Help with strings

the syntax tag for python should really have a capital Smile
Leela




PostPosted: 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.
Python:
filename = raw_input('Enter your file: ')
ext = filename[filename.rfind(".") + 1:]
print 'Your extention is', ext

Smile[/quote]
Thanks! What is rfind? I only know 'find'.
BigBear




PostPosted: 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)
Leela




PostPosted: 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!
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: