DEV Community

Vasanthakumari N
Vasanthakumari N

Posted on

Python Day 19: Python String Methods and Type Conversion

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())
Enter fullscreen mode Exit fullscreen mode
Output:

hello python
Enter fullscreen mode Exit fullscreen mode

upper()

Converts all characters into uppercase.

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

HELLO PYTHON
Enter fullscreen mode Exit fullscreen mode

capitalize()

Converts the first character into uppercase and the remaining characters into lowercase.

text = "python programming"
print(text.capitalize())
Enter fullscreen mode Exit fullscreen mode
Output:

Python programming
Enter fullscreen mode Exit fullscreen mode

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())

Enter fullscreen mode Exit fullscreen mode
Output:

['Python', 'is', 'easy']
Enter fullscreen mode Exit fullscreen mode

We can also specify a separator.

Example 1: Date using /

text = "20/11/2026"
print(text.split("/"))
Enter fullscreen mode Exit fullscreen mode
Output:

['20', '11', '2026']
Enter fullscreen mode Exit fullscreen mode

Example 2: Date using -

text = "20-11-2026"
print(text.split("-"))
Enter fullscreen mode Exit fullscreen mode
Output:

['20', '11', '2026']
Enter fullscreen mode Exit fullscreen mode

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"))
Enter fullscreen mode Exit fullscreen mode

Output:

9
Enter fullscreen mode Exit fullscreen mode

Finding the First Occurrence

text = "Dhoni Dhoni Dhoni"
print(text.find("Dhoni"))
Enter fullscreen mode Exit fullscreen mode
Output:

0
Enter fullscreen mode Exit fullscreen mode

find() returns the index of the first occurrence.

When the Text Is Not Found


text = "Dhoni Dhoni Dhoni"
print(text.find("Doni"))
Enter fullscreen mode Exit fullscreen mode
Output:

-1
Enter fullscreen mode Exit fullscreen mode

Understanding find()

text = "apple"
print(text.find("p"))
Enter fullscreen mode Exit fullscreen mode
  • 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"))
Enter fullscreen mode Exit fullscreen mode
Output:

2
Enter fullscreen mode Exit fullscreen mode

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))

Enter fullscreen mode Exit fullscreen mode
Output:

['a', 'p', 'p', 'l', 'e']
Enter fullscreen mode Exit fullscreen mode

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)))
Enter fullscreen mode Exit fullscreen mode
Output:

<class 'str'>
<class 'list'>
Enter fullscreen mode Exit fullscreen mode

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()))
Enter fullscreen mode Exit fullscreen mode
Output:

11
Python
6
Enter fullscreen mode Exit fullscreen mode

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()))
Enter fullscreen mode Exit fullscreen mode
Output:
<class 'str'>
<class 'int'>
<class 'list'>
<class 'NoneType'>
Enter fullscreen mode Exit fullscreen mode

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"))

Enter fullscreen mode Exit fullscreen mode
Output:

False
Enter fullscreen mode Exit fullscreen mode

Why?

Python strings are case-sensitive.

virat and Virat are different.

Correct example:


text = "virat kohli"
print(text.startswith("virat"))
Enter fullscreen mode Exit fullscreen mode
Output:

True
Enter fullscreen mode Exit fullscreen mode

9. endswith() Method

The endswith() method checks whether a string ends with a particular value.

text = "rohit sharma"

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

Output:

True

Enter fullscreen mode Exit fullscreen mode

String Slicing with endswith()


text = "rohit sharma"

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

Output:

sharma
sharma
True
Enter fullscreen mode Exit fullscreen mode

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())
Enter fullscreen mode Exit fullscreen mode
Output:

False
Enter fullscreen mode Exit fullscreen mode

Why? Because = is a special character.

Another example:

phone = '=abcd2121$%^&'
print(phone.isalnum())
Enter fullscreen mode Exit fullscreen mode

Output:

False
Enter fullscreen mode Exit fullscreen mode

isalpha()

Returns True if all characters are alphabets.

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

Output:

True

Enter fullscreen mode Exit fullscreen mode

Example with numbers:


text = "Python123"
print(text.isalpha())
Enter fullscreen mode Exit fullscreen mode

Output:

False
Enter fullscreen mode Exit fullscreen mode

isdecimal()

Returns True if all characters are decimal digits.

standard = "42"
print(standard.isdecimal())
Enter fullscreen mode Exit fullscreen mode
Output:

True

Enter fullscreen mode Exit fullscreen mode

isdigit()

Returns True if all characters are digits, including certain Unicode digit characters such as superscript digits.

standard = "42"
print(standard.isdigit())
Enter fullscreen mode Exit fullscreen mode
Output:

True
Enter fullscreen mode Exit fullscreen mode

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())

Enter fullscreen mode Exit fullscreen mode
Output:

False
False
True
Enter fullscreen mode Exit fullscreen mode

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())

Enter fullscreen mode Exit fullscreen mode

Output:


True True
False True
False False
Enter fullscreen mode Exit fullscreen mode

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)))
Enter fullscreen mode Exit fullscreen mode

Output:

<class 'str'>
<class 'list'>
Enter fullscreen mode Exit fullscreen mode

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)