1. Changing the Case of a String
Python provides different methods to change the case of a string.
lower()
Converts all characters into lowercase.
text = "Hello PYTHON"
print(text.lower())
Output:
hello python
upper()
Converts all characters into uppercase.
text = "Hello Python"
print(text.upper())
Output:
HELLO PYTHON
capitalize()
Converts the first character into uppercase and the remaining characters into lowercase.
text = "python programming"
print(text.capitalize())
Output:
Python programming
2. split() Method
The split() method divides a string into a list of smaller strings.
If we don't provide a separator, Python splits the string using whitespace.
text = "Python is easy"
print(text.split())
Output:
['Python', 'is', 'easy']
We can also specify a separator.
Example 1: Date using /
text = "20/11/2026"
print(text.split("/"))
Output:
['20', '11', '2026']
Example 2: Date using -
text = "20-11-2026"
print(text.split("-"))
Output:
['20', '11', '2026']
Remember: split() returns a list.
3. find() Method
The find() method searches for a substring and returns its index (position).
If the substring is not found, it returns -1.
text = "Mahendra Singh Dhoni"
print(text.find("Singh"))
Output:
9
Finding the First Occurrence
text = "Dhoni Dhoni Dhoni"
print(text.find("Dhoni"))
Output:
0
find() returns the index of the first occurrence.
When the Text Is Not Found
text = "Dhoni Dhoni Dhoni"
print(text.find("Doni"))
Output:
-1
Understanding find()
text = "apple"
print(text.find("p"))
- Number of arguments passed: 1
- Object name: text
- Method name: find()
- Return data type: int
The find() method can also accept optional start and end positions.
4. count() Method
The count() method returns how many times a particular substring occurs.
text = "apple apple mango"
print(text.count("apple"))
Output:
2
Here, apple appears two times.
5. Converting a String into a List
We can convert a string into a list using the list() function.
text = "apple"
print(list(text))
Output:
['a', 'p', 'p', 'l', 'e']
Here, each character becomes an individual element in the list.
We can verify the data type:
text = "apple"
print(type(text))
print(type(list(text)))
Output:
<class 'str'>
<class 'list'>
This is one way to demonstrate that a string object can be converted into a list object.
6. strip() Method
The strip() method removes whitespace from the beginning and end of a string.
text = " Python "
print(len(text))
print(text.strip())
print(len(text.strip()))
Output:
11
Python
6
Important: strip() returns a new string. It does not modify the original string.
7. Function Return Data Type
A function or method can return a value of a particular data type.
For example:
text = " Python "
print(type(text.strip()))
print(type(text.find("Python")))
print(type(text.split()))
print(type(print()))
Output:
<class 'str'>
<class 'int'>
<class 'list'>
<class 'NoneType'>
Why does print() return None?
Because the print() function displays a value but does not return that displayed value.
Learning point: A function's return type depends on the value it returns.
8. startswith() Method
The startswith() method checks whether a string starts with a particular value.
It returns True or False.
text = "virat kohli"
print(text.startswith("Virat"))
Output:
False
Why?
Python strings are case-sensitive.
virat and Virat are different.
Correct example:
text = "virat kohli"
print(text.startswith("virat"))
Output:
True
9. endswith() Method
The endswith() method checks whether a string ends with a particular value.
text = "rohit sharma"
print(text.endswith("sharma"))
Output:
True
String Slicing with endswith()
text = "rohit sharma"
print(text[6:])
print(text[-6:])
print(text.endswith("sharma"))
Output:
sharma
sharma
True
10. Checking String Content
Python provides several methods to check whether a string contains only certain types of characters.
isalnum()
Returns True if all characters are alphabets or numbers, with at least one character.
phone = '=abcd2121'
print(phone.isalnum())
Output:
False
Why? Because = is a special character.
Another example:
phone = '=abcd2121$%^&'
print(phone.isalnum())
Output:
False
isalpha()
Returns True if all characters are alphabets.
text = "Python"
print(text.isalpha())
Output:
True
Example with numbers:
text = "Python123"
print(text.isalpha())
Output:
False
isdecimal()
Returns True if all characters are decimal digits.
standard = "42"
print(standard.isdecimal())
Output:
True
isdigit()
Returns True if all characters are digits, including certain Unicode digit characters such as superscript digits.
standard = "42"
print(standard.isdigit())
Output:
True
isnumeric()
Returns True if all characters are numeric characters, including some numeric characters that are not decimal digits, such as certain fractions and Roman numerals.
standard = "VIII"
print(standard.isdecimal())
print(standard.isdigit())
print(standard.isnumeric())
Output:
False
False
True
Difference Between isdecimal(), isdigit(), and isnumeric()
| Method | What it checks |
|---|---|
isdecimal() |
Decimal digits only |
isdigit() |
Decimal digits and certain other digit characters |
isnumeric() |
A wider range of numeric characters |
standard = "42"
superscript = "2³"
fraction = "¾"
print(standard.isdecimal(), standard.isdigit())
print(superscript.isdecimal(), superscript.isdigit())
print(fraction.isdecimal(), fraction.isdigit())
Output:
True True
False True
False False
11. Type Conversion – String to List
In Python, we can convert one type of object into another type when a suitable conversion is available.
Examples:
text = "apple"
print(type(text))
print(type(list(text)))
Output:
<class 'str'>
<class 'list'>
Other examples:
- int("100") converts a numeric string into an integer.
- float("10.5") converts a numeric string into a float.
- str(100) converts an integer into a string.
- list("apple") converts a string into a list of characters.
Important: Not every conversion is possible. For example, int("apple") raises a ValueError.
Top comments (0)