Python
About Lesson

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 the above code will be:

Hello World

 

casefold()

casefold() method converts the string into lowercase.

For example:

str = "INFOVISTARDOTIN"
print(str.casefold())

The output of the above code will be:

infovistardotin

 

count()

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

For example:

str = "InfovistarDotIn"
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 = "InfovistarDotIn"
print(str.endswith("In"))

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 = "InfovistarDotIn"
print(str.find("In"))

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 = "InfovistarDotIn"
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 = "InfovistarDotIn"
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 = "InfovistarDotIn"
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 = "InfovistarDotIn"
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 = "InfovistarDotIn"
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?In"
print(str.replace("?", "Dot"))

The output of the above code will be:

InfovistarDotIn

 

upper()

upper() method converts a string into an upper case.

For example:

str = "InfovistardotIn"
print(str.upper())

The output of the above code will be:

INFOVISTARDOTIN