- String Methods – Meaning and Examples String methods are built-in methods used to perform operations on strings
text = "Hello PYTHON"
print(text.lower())
output : hello python # Convert to lowercase
text = "Hello Python"
print(text.upper())
output : HELLO PYTHON # Convert to uppercase
text = "python programming"
print(text.capitalize())
output : Python programming # First letter capital
text = "Python is easy"
print(text.split())
output : ['Python', 'is', 'easy'] # Split string into a list
text = "I like Java"
print(text.replace("Java", "Python"))
output : I like Python # Replace old text with new text
text = "Mahendra Singh Dhoni"
print(text.find("a"))
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.
- 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
- 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
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}
- 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()))
output : 11
<class 'NoneType'
strip - return datatype #Removes spaces from the beginning and end of a strip
- What is an Argument? Argument means the value passed inside a function or method
text = "virat kohli"
print(text.startswith("Virat"))
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"))
output : True
Here:
- Method: startswith()
- Object: text
- Argument: "virat"
- Return datatype: bool
- Output: True
- 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"))
output : True
text = "rohit sharma"
print(text[6:])
print(text[-6:])
print(text.endswith("sharma"))
output : sharma
sharma
True
- 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())
output : True
False
True
standard = "Ⅷ"'
print(standard.isdecimal())
print(standard.isdigit())
print(standard.isnumeric())
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'''
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)