Substitute for slice function
I'm new to python and the iOS app but I have an application requirement.
I want I extract a single character from a string and have been led to understand I need to use the slice function, ie string[a:b] but after a quick search I find it can not be done in the iOS app.
Apologies if this has been covered but I wasn't sure how to search the forum for this.
Thanks in advance
eliskan175 last edited by
This can definitely be done in Pythonista, I just tested it myself to make sure because if it didn't work that would be a big problem for me.
Probably you were setting up your string wrong, or accessing it improperly. Here's a solution that does work, test it in Pythonista and you will see!:
test = "Blahblahblah"
The output should be simply "Blah", followed by a second "blah"
Another way you can access individual characters of a string is like this:
for s in test: print s
This example prints each letter individually.
Another example to help you with string manipulation.. if you're looking for a specific WORD inside a string, you can use this:
test = "Just a string with a word we want. In this case, rainbows."
if "rainbows" in test: print "Our string has rainbows in it!"
if "nachos" in test: print "Share some nachos with me."
You can combine these functions together to get more control:
for index, s in enumerate(test):
if s=="h": print test[:index+1]
This example outputs:
I hope this helped.
Thanks for the help. Your code worked fine. I must be writing the script incorrectly-
testword = "abcdefghijklmnop"
partstring = testword[3:3]
print "testword is "+testword
print "partstring is "+testword[3:3]
print ("got:"+partstring+ " from "+testword)
Gives me the following (including the blank 4th line)-
testword is abcdefghijklmnop
got: from abcdefghijklmnop
As I said, total newbie, so thanks for your patience with me
Think I've just found my fix.
Having been learning lua simultaneously as python I have been referencing as string[start:length] where I should be using string[start:end] not realising that if start and end are the same it is null.
Apologies for my dumbness