Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Is there an equivalent of mid, right, and left from BASIC?

Tags:

python

basic

I want to do something like this:

    >>> mystring = "foo"     >>> print(mid(mystring)) 

Help!

like image 453
pythonprogrammer Avatar asked Mar 23 '14 02:03

pythonprogrammer


People also ask

What is left and right in Python?

There are built-in functions in Python for "right" and "left", if you are looking for a boolean result. str = "this_is_a_test" left = str.startswith("this") print(left) > True right = str.endswith("test") print(right) > True. Follow this answer to receive notifications.

What is the equivalent of substring in Python?

Python has no substring methods like substring() or substr(). Instead, we use slice syntax to get parts of existing strings.

Is there a like operator in Python?

Python string contains or like operator This method implements check of a given list if it is part of another list. This can be used as a filter for messages.

How do you get a certain part of a string in Python?

Python provides different ways and methods to generate a substring, to check if a substring is present, to get the index of a substring, and more. start - The starting index of the substring. stop - The final index of a substring. step - A number specifying the step of the slicing.


1 Answers

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] 
like image 61
Andy W Avatar answered Sep 16 '22 23:09

Andy W