In this specific case:
val[val.index('*')+1:]
Find the first occur position of *, and take a slice from there.
or:
val.split('*', 1)[-1]
The tells to split string only once with delimiter, if you want everything after the first *.
In this specific case:
val[val.index('*')+1:]
Find the first occur position of *, and take a slice from there.
or:
val.split('*', 1)[-1]
The tells to split string only once with delimiter, if you want everything after the first *.
>>> val = 's2*20'
>>> val.split('*')[1]
'20'
Depending on what you want, you may want to check for '*' inside your string, or just taking everything after the first occurrence of '*', but hard to guess without more info. This might be a plausible scenario:
>>> def rest(s):
... return s.split('*',1) if '*' in s else s
...
>>> rest('hi')
'hi'
>>> rest('hi there * wazzup * man')
['hi there ', ' wazzup * man']
Edit: as pointed out in the comment by @Jon (no, not Skeet), using partition is better in every way.
>>> val = 's2*20'
>>> val.partition('*')[2]
'20'
>>> val = 's2-20'
>>> val.partition('*')[2]
''
It's smoother and performs surprisingly good - in fact a lot better than split:
>>> import timeit
>>> timeit.timeit('"s2*20".split("*")')
0.22846507105495942
>>> timeit.timeit('"s2-20".split("*")')
0.1685205617091654
>>> timeit.timeit('"s2*20".partition("*")')
0.1475598294296372
>>> timeit.timeit('"s2-20".partition("*")')
0.09512975462482132

Videos
>>> x = "Hello World!"
>>> x[2:]
'llo World!'
>>> x[:2]
'He'
>>> x[:-2]
'Hello Worl'
>>> x[-2:]
'd!'
>>> x[2:-2]
'llo Worl'
Python calls this concept "slicing" and it works on more than just strings. Take a look here for a comprehensive introduction.
Just for completeness as nobody else has mentioned it. The third parameter to an array slice is a step. So reversing a string is as simple as:
some_string[::-1]
Or selecting alternate characters would be:
"H-e-l-l-o- -W-o-r-l-d"[::2] # outputs "Hello World"
The ability to step forwards and backwards through the string maintains consistency with being able to array slice from the start or end.
There's a builtin method find on string objects.
s = "Happy Birthday"
s2 = "py"
print(s.find(s2))
Python is a "batteries included language" there's code written to do most of what you want already (whatever you want).. unless this is homework :)
find returns -1 if the string cannot be found.
Ideally you would use str.find or str.index like demented hedgehog said. But you said you can't ...
Your problem is your code searches only for the first character of your search string which(the first one) is at index 2.
You are basically saying if char[0] is in s, increment index until ch == char[0] which returned 3 when I tested it but it was still wrong. Here's a way to do it.
def find_str(s, char):
index = 0
if char in s:
c = char[0]
for ch in s:
if ch == c:
if s[index:index+len(char)] == char:
return index
index += 1
return -1
print(find_str("Happy birthday", "py"))
print(find_str("Happy birthday", "rth"))
print(find_str("Happy birthday", "rh"))
It produced the following output:
3
8
-1