slices to the rescue :)
def left(s, amount):
return s[:amount]
def right(s, amount):
return s[-amount:]
def mid(s, offset, amount):
return s[offset:offset+amount]
Answer from Andy W on Stack Overflowslices to the rescue :)
def left(s, amount):
return s[:amount]
def right(s, amount):
return s[-amount:]
def mid(s, offset, amount):
return s[offset:offset+amount]
If I remember my QBasic, right, left and mid do something like this:
>>> s = '123456789'
>>> s[-2:]
'89'
>>> s[:2]
'12'
>>> s[4:6]
'56'
http://www.angelfire.com/scifi/nightcode/prglang/qbasic/function/strings/left_right.html
Can someone tell me what mid does in python?
Middle function on python - Stack Overflow
How to return the middle character of a string in python? - Stack Overflow
Passing data mid-function back to function Python - Stack Overflow
context. Given a string, return a new string where the first and last chars have been exchanged.
their answer
def front_back(str):
if len(str) <= 1:
return str
mid = str[1:len(str)-1] # can be written as str[1:-1]
# last + mid + first
return str[len(str)-1] + mid + str[0]
» pip install python-mid
Here's an example of how I would approach it:
def mid(string):
if len(string) % 2 == 0:
return ""
else:
offset = int(len(string) / 2)
return string[offset: offset + 1]
mid("abc")
mid("asaba")
Your code fails on edge cases, specifically when given the empty string, but I found during my tests that it works for strings like "asaba", "aaaa", and "abc" but throws the error when given "". The reason for this is because lastnum = enum[-1][0] + 1 will give an index that does not exist for the empty string. The way to fix this would be to add a condition at the beginning of your function that checks if it's an empty string, so like this:
if len(string) == 0:
return ""
Add the following to the end of your test() function:
`return foo`
then you can print the variable in work() like this
print test(data)
foo is only defined in the "scope" of the function test(), since that is where you created it. The function work() has no knowledge of the variable foo, as it is undefined outside of the function test(). So, test() has to return the variable foo to the place that called test(), which is the line test(data) in work().
So, yes, add return foo to the end of test().
Edit:
When you say test(data), that is basically saying sum([1,2,3]). You've called a function, but you're not doing anything with the result, you're not assigning it. You have to say new_variable = test(data). This means, from the perspective of work(): "call the function test() and have it do its thing. I don't care what's going on inside of test(), but I am expecting to it to spit something out at me when it is done. I will assign that something to a variable in my scope so I can use it later".
It is just like saying x = sum([1,2,3]). sum is a function that does something inside of it, you don't really care what, you just know that it should return a sensible value that you will assign to x to use later.
Edit2: Also, as it stands, test() is going to return a boolean for foo, since you use the == operator rather than the assignment operator =.