For any queries you can reach us at infovistarindia@gmail.com / WhatsApp us: +919158876092

Useful String Methods in Python

Useful String Methods in Python

  • capitalize()
  • casefold()
  • count()
  • endswith()
  • find()
  • isalpha()
  • isdigit()
  • isidentifier()
  • islower()
  • isupper()
  • replace()
  • upper()

capitalize()

capitalize() method converts the first character to upper case.

For example:
str = "hello world"
print(str.capitalize())

The output for above code will be:

Hello World

casefold()

casefold() method converts the string into lower case.

For example:
str = "INFOVISTARDOTCOM"
print(str.casefold())

The output of the above code will be:

infovistardotcom

count()

count() method returns the number of times a specified character occures.

For example:
str = "InfovistarDotCom"
print(str.count("o"))

The output of the above code will be:

3

endswith()

endswith() method returns True if the string ends with specified character or values.

For example:
str = "InfovistarDotCom"
print(str.endswith("Com"))

The output of the above code will be:

True

find()

find() method searches for a specified value and returns the position of where it was found.

For example:
str = "InfovistarDotCom"
print(str.find("Com"))

The output of the above code will be:

12

isalpha()

isalpha() method returns True if all characters in the string are in the alphabet.

For example:
str = "InfovistarDotCom"
print(str.isalpha())

The output of the above code will be:

True

isdigit()

isdigit() method returns True if all characters in the string are in the digits.

For example:
str = "InfovistarDotCom"
print(str.isdigit())

The output of the above code will be:

False

isidentifier()

isidentifier() method returns True if the string is an identifier.

For example:
str = "InfovistarDotCom"
print(str.isidentifier())

The output of the above code will be:

True

islower()

islower() method returns True if characters in the string are lower case.

For example:
str = "InfovistarDotCom"
print(str.islower())

The output of the above code will be:

False

isupper()

isupper() method returns True if characters in the string are upper case.

For example:
str = "InfovistarDotCom"
print(str.isupper())

The output of the above code will be:

False

replace()

replace() method returns a string where a specified value is replaced.

For example:
str = "Infovistar?Com"
print(str.replace("?", "Dot"))

The output of the above code will be:

InfovistarDotCom

upper()

upper() method converts a string into upper case.

For example:
str = "Infovistar?Com"
print(str.upper())

The output of the above code will be:

INFOVISTARDOTCOM