String Methods in Python: Part-2

This post shall cover some of the other commonly used string methods. You can read the first part here - String Methods in Python: Part-1

The following methods shall be covered here-

  • count()
  • find()
  • title()
  • strip()

1. count()-

The count() method returns the number of times a specific value appears in the string. Here, the parenthesis do not remain empty, but has the value whose frequency you want to check.

For example;

string="I like apples. I like bananas too."
print(string.count("I"))

The output will be;

2

2. find()

The find() method searches the string for the value passed as an argument and returns the location of the value in the string.

For example;

string="Hello World!"
print(string.find("World!"))

The output;

6

3. title()

The title() method converts the starting letter of the given string into uppercase. For example;

string="hello World!"
print(string.title())

The interpreter will show;

Hello World!

4. strip()

The strip() method basically removes the trailing (at the end of the string) or leading (at the start of the string) characters from a string.

You can specify the character that you want removed by passing it as an argument. If none is passed, the leading and trailing whitespaces are removed.

Let us look at an example without any arguments-

string="      Hello      "
print(string.strip())

The output-

Hello

This example contains a specific character given as the argument to strip()-

string=",,,,,Hello,,,,,"
print(string.strip(','))

The output;

Hello


That is all for this post. Share it with any code-minded people you know and help us grow!

#LetsCode

Until next time,

Aarav Iyer

Aarav Iyer

I am a technology and programming enthusiast, currently a high school student. I love drawing and am quite interested in aeronautics and astrophysics too. My favourite pastimes are reading books, blogging and skywatching with my telescope.

4 Comments

Next Post Previous Post