
-----------------------------------
BigBear
Sat Mar 21, 2009 1:07 pm

[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 filename = raw_input ("Enter a file name: ")
num = 

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.

-----------------------------------
Insectoid
Sat Mar 21, 2009 1:20 pm

RE:[Python] Help with strings
-----------------------------------
Why not just get the last character of the string and back up 3 spaces?


for i in range (len (filename)-3, len (filename):
    print filename

btw, I'm don't know any python beyond raw_input, so if something's wrong there, whatever.

-----------------------------------
BigBear
Sat Mar 21, 2009 1:27 pm

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.

-----------------------------------
Insectoid
Sat Mar 21, 2009 1:32 pm

RE:[Python] Help with strings
-----------------------------------
[code]
for i in range (len(filename),num): 
[/code]
should be
[code]
for i in range (num, len(filename)): 
[/code]

-----------------------------------
[Gandalf]
Sat Mar 21, 2009 6:31 pm

RE:[Python] Help with strings
-----------------------------------
Or, using slicing:
[code]print filename[num:][/code]

-----------------------------------
BigBear
Sat Mar 21, 2009 7:17 pm

Re: RE:[Python] Help with strings
-----------------------------------


Yeah I tried it that way to see the difference in errors and didn't change it back.

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.

fname = raw_input("Enter your full name.")
num = 

Fixed quote.

-----------------------------------
[Gandalf]
Sun Mar 22, 2009 12:15 am

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? :)

-----------------------------------
Leela
Sat May 02, 2009 1:28 pm

Re: [Python] Help with strings
-----------------------------------
Can someone advise me whether my solution to this problem is all right?
filename = raw_input ('Enter your file: ')
extention = 
Great thanks!

-----------------------------------
[Gandalf]
Sat May 02, 2009 2:53 pm

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.
filename = raw_input('Enter your file: ')
ext = filename
:)

-----------------------------------
BigBear
Sat May 02, 2009 3:32 pm

RE:[Python] Help with strings
-----------------------------------
the syntax tag for python should really have a capital :)

-----------------------------------
Leela
Sat May 02, 2009 4:29 pm

Re: RE:[Python] Help with strings
-----------------------------------
filename = raw_input('Enter your file: ')
ext = filename
:)[/quote]
Thanks! What is rfind? I only know 'find'.

-----------------------------------
BigBear
Sat May 02, 2009 4:41 pm

Re: [Python] Help with strings
-----------------------------------
rfind(...)
 |      S.rfind(sub 

Type help(str)

-----------------------------------
Leela
Sat May 02, 2009 10:23 pm

Re: [Python] Help with strings
-----------------------------------
rfind(...)
 |      S.rfind(sub 

Type help(str)

Thanks!
