DEV Community

Vinoth
Vinoth

Posted on

Python String Methods – Complete Guide with Examples

  1. String Methods – Meaning and Examples String methods are built-in methods used to perform operations on strings

text = "Hello PYTHON"
print(text.lower())
Enter fullscreen mode Exit fullscreen mode

output : hello python # Convert to lowercase


text = "Hello Python"
print(text.upper())
Enter fullscreen mode Exit fullscreen mode

output : HELLO PYTHON # Convert to uppercase


text = "python programming"
print(text.capitalize())

Enter fullscreen mode Exit fullscreen mode

output : Python programming # First letter capital

text = "Python is easy"
print(text.split())

Enter fullscreen mode Exit fullscreen mode

output : ['Python', 'is', 'easy'] # Split string into a list

text = "I like Java"
print(text.replace("Java", "Python"))
Enter fullscreen mode Exit fullscreen mode

output : I like Python # Replace old text with new text

text = "Mahendra Singh Dhoni"
print(text.find("a"))
Enter fullscreen mode Exit fullscreen mode

output : 1 # Find the position of a character/text
eg : m a h e n d r a
0 1 2 3 4 5 6 7

1) find - How many arguments are passed - 1
2) find - Object Name? text
3) find - return datatype? int

Remember:

  • find() returns -1 if the text is not found.
  • Python indexing starts from 0.
  1. What is an Object? Definition: An object is an instance of a class.

text = "apple"
print(list(text))

output :
Here:

  • text → Variable name
  • "apple" → String object
  • str → Class / datatype
  1. Can we convert one object type into another? True Python supports Type Conversion.
text = "apple" 
#object - how do you prove this technically?
print(type(text))
print(list(text)) #list 

Enter fullscreen mode Exit fullscreen mode

output :
['a', 'p', 'p', 'l', 'e']

Here:

  • str → Original datatype
  • list() → Type conversion function
  • list → New datatype

str, list, tuple, dict, set:
Five important Python datatypes
Datatype Example
str "Python"
list [10, 20, 30]
tuple (10, 20, 30)
dict {"name": "Vinoth"}
set {10, 20, 30}

  1. What is a Function? Definition: A function is a reusable block of code that performs a specific task.
text = "   Python   "
print(len(text))
print(type(text.strip())) 
print(type(print()))

Enter fullscreen mode Exit fullscreen mode

output : 11

<class 'NoneType'
strip - return datatype #Removes spaces from the beginning and end of a strip

  1. What is an Argument? Argument means the value passed inside a function or method
text = "virat kohli"
print(text.startswith("Virat"))
Enter fullscreen mode Exit fullscreen mode

output : False
Why False?
Because Python is case-sensitive.

Here:

  • Method: startswith()
  • Object: text
  • Argument: "Virat"
  • Return datatype: bool
  • Output: False

text = "virat kohli"
print(text.startswith("virat"))

Enter fullscreen mode Exit fullscreen mode

output : True
Here:

  • Method: startswith()
  • Object: text
  • Argument: "virat"
  • Return datatype: bool
  • Output: True
  1. startswith() and endswith() These methods check whether a string starts or ends with a particular text
text = "rohit sharma"

print(text.startswith("rohit"))
print(text.endswith("sharma"))
Enter fullscreen mode Exit fullscreen mode

output : True


text = "rohit sharma"
print(text[6:])
print(text[-6:])
print(text.endswith("sharma"))
Enter fullscreen mode Exit fullscreen mode

output : sharma
sharma
True

  1. isalnum(), isalpha(), isdecimal() These methods check the type of characters inside a string. Method Meaning isalnum() Only alphabets and numbers isalpha() Only alphabets isdecimal() Only decimal digits
phone = '121212121'
print(phone.isalnum())
print(phone.isalpha())
print(phone.isdecimal())
Enter fullscreen mode Exit fullscreen mode

output : True
False
True

standard = "Ⅷ"'
print(standard.isdecimal())
print(standard.isdigit())
print(standard.isnumeric())
Enter fullscreen mode Exit fullscreen mode

output : False
False
True

Easy way to remember
isdecimal()
Normal decimal digits
12345
Only decimal digit characters.

isdigit()
Decimal + special digits
12345 + ² + ³
Includes superscript digits.

isnumeric()
Digits + fractions + other numeric characters
12345 + ² + ¾ + Ⅷ
The widest numeric-character check.


name = 'velachery chennai'
statement = '''Dhoni is a good player.  he is our captain.  He is a Wicketkeeper' 'Dhoni was a good player.  he was our captain.  He was a  Wicketkeeper'''
Enter fullscreen mode Exit fullscreen mode

print(name)
print(statement)
output : velachery chennai
Dhoni is a good player. he is our captain. He is a Wicketkeeper'
'Dhoni was a good player.
he was our captain. He was a Wicketkeeper

Top comments (0)