Python string find() examples

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BarryA
    New Member
    • Jun 2022
    • 19

    Python string find() examples

    I'm looking for examples, but I'm not finding any.
    Are there any examples on the internet? I'd like to know what it returns when it can't find something, as well as how to specify the starting and ending points, which I assume will be 0, -1.
    Code:
    >>> x = "Hello World"
    >>> x.find('World')
    6
    >>> x.find('Aloha');
    -1
    Could you please help me? But I'm not convinced.
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    I'd like to know what it returns when it can't find something
    -1

    how to specify the starting and ending points
    Using the optional parameters. One could use positive or negative indexing.

    Could you please help me? But I'm not convinced.
    Share the examples.

    Comment

    • BarryA
      New Member
      • Jun 2022
      • 19

      #3
      Hey, thanks for the clarification! It truly cleared my doubt. Also, I need assistance with some coding here. I intended to use a dictionary to construct the switch case pattern in Python, as this tutorial suggested, but here's the problem:
      Code:
        # Type can be either create or update or ..
        message = { 'create':msg(some_data),
                    'update':msg(other_data)
                    # can have more
                  }
      
        return message(type)
      However, it does not work for me since some data or other data might be None (it produces an error if it is None) and the message function must be basic (I do not want to include any conditions).

      The issue here is that the method message() is called each time the dict is filled. Unlike the switch case pattern, which normally does not run the code in the case until it is a match in other programming languages.

      Comment

      • dev7060
        Recognized Expert Contributor
        • Mar 2017
        • 656

        #4
        Code:
        message(type)
        How would it know if you're trying to retrieve a value or call a function?

        However, it does not work for me since some data or other data might be None (it produces an error if it is None) and the message function must be basic (I do not want to include any conditions).

        The issue here is that the method message() is called each time the dict is filled. Unlike the switch case pattern, which normally does not run the code in the case until it is a match in other programming languages.
        Show the complete code.

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          BarryA - please start a new thread for a new question
          I can split post#3 into it's own thread if you would like...

          Comment

          • Vanisha
            New Member
            • Jan 2023
            • 26

            #6
            The find() method in Python is used to find the starting index of the first occurrence of a substring within a string. If the substring is not found in the string, it returns -1. Here are some examples of how to use the find() method:

            Example: Find the index of a substring in a string
            Code:
            string = "hello world"
            substring = "world"
            index = string.find(substring)
            print(index)  
            #Output: 6
            Last edited by zmbd; Mar 1 '23, 05:10 AM. Reason: [z{removed link spam}{placed code tags}]

            Comment

            Working...