Search code examples
pythonstringstartswith

How do I get the beginning of a string in Python?


So... I tried to get what my string starts with and I tried to see if I can get what my string starts with, with the startswith function. But as I'm trying to use startswith function it just returning True or False. This is how I tried:

str1 = "sdf fds dfs"
str1.startswith()

Solution

  • To add to the above answers, if you want the first word you can use str1.split(" ").

    e.g.

    str1 = "sdf fds dfs"
    str1 = str1.split(" ")
    print(str1[0])
    

    prints sdf