Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. String manipulation in python
  5. Function and methods in string in python

Function and methods in string in python

Python also offers many built-in functions and methods for string manipulation.

  • capitalize()- This function returns a copy of the string with its first character capitalized.

e.g.,

s="python"
s.capitalize()
Output- Python
  • upper()- This method returns a string with lower case characters replaced by corresponding uppercase characters.

e.g.,

s="python"
s.upper()
Output- PYTHON
  • lower()- This method results in a string with uppercase characters replaced by corresponding lowercase characters.

e.g.,

s="PYTHON"
s.lower()
Output- python
  • title()- This method results in a string with the first character of each word converted to uppercase.

e.g.,

s="hello, world!"
s.title()
Output- Hello, World!
  • isalpha()- This method returns true if all the characters in a string are alphabetic., otherwise returns false.

e.g.,

s="python"
s.isalpha()
Output- True
  • isdigit()- This method returns true if all the characters in the string are digits. Otherwise, returns false.

e.g.,

s="123"
s.isdigit()
Output- True
  • islower()- This method returns true if all the characters in a string are lowercase characters else returns false.

e.g.,

s="python"
s.islower()
Output- True
  • isupper()- This method returns true if all the characters in a string are uppercase characters else, returns false.

e.g.,

s="python"
s.isupper()
Output- False
  • strip()- This method is used to remove extra whitespace from beginning and end of string.

e.g.,

s="      python           "
s.strip()
Output- python
  • lstrip()- This method is used to remove extra whitespace from left in string.

e.g.,

s="        python"
s.lstrip()     
output- python
  • rstrip()- This method is used to remove extra whitespace from right in string.

e.g.,

s="python      "
s.rstrip()
Output- python

This is all about functions and methods in strings in python.

Was this article helpful to you? Yes No

How can we help?