Python: Basic String Functions Cheat Sheet with Examples

Python: Basic String Functions Cheat Sheet with Examples



1. upper()

The upper() function converts all characters in a string to uppercase.

Example 1:


text = "hello world"
print(text.upper())

Output:


HELLO WORLD

⚠️ Special Cases: Non-alphabetic characters are unaffected:


text = "1234 @hello!"
print(text.upper())

Output:


1234 @HELLO!

2. lower() Function

The lower() function converts all characters in a string to lowercase.

Example 1:


text = "HELLO World 123"
print(text.lower())

Output:


hello world 123

3. title() Function

The title() function converts the first character of each word to uppercase and the rest to lowercase.

Example 1:


text = "welcome to python programming"
print(text.title())

Output:


Welcome To Python Programming

⚠️ Point to Remember:

It treats any character after a non-alphabetic symbol as the start of a new word:


text = "hello-world_from python"
print(text.title())

Output:


Hello-World_From Python

4. strip() Function

The strip() function removes leading and trailing whitespaces (spaces, \n, \t) from a string.

Example 1:


text = "  Hello World  "
print(text.strip())

Output:


Hello World

⚠️ Point to Remember:

You can also remove specific characters using:


text = "###Hello###"
print(text.strip("#"))

Output:


Hello

5. replace() Function

The replace() function replaces all occurrences of a substring with another string.

Example 1:


text = "apple apple"
print(text.replace("apple", "banana"))

Output:


banana banana

✅ Important Example:


sentence = "I love Python. Python is easy."
print(sentence.replace("Python", "Java"))

Output:


I love Java. Java is easy.

6. find() Function

The find() function returns the index of the first occurrence of a substring. Returns -1 if not found.

Example 1:


text = "Hello"
print(text.find("l"))

Output:


2

⚠️ Point to Remember:

If the substring is not found, it returns -1:


text = "Hello"
print(text.find("z"))

Output:


-1

7. count() Function

The count() function returns the number of times a substring appears in a string.

Example 1:


text = "banana"
print(text.count("a"))

Output:


3

⚠️ Point to Remember:

It is case-sensitive:


text = "Python python PYTHON"
print(text.count("Python"))

Output:


1

8. \t (Tab)

\t is used to insert a horizontal tab space in a string.

Example 1:


print("Name:\tAyush")

Output:


Name:	Ayush

9. \n (New Line)

\n is used to insert a new line (line break) in a string.

Example 1:


print("Line1\nLine2")

Output:


Line1
Line2

10. capitalize() Function

The capitalize() function converts the first character to uppercase and the rest to lowercase.

Example 1:


text = "hello world"
print(text.capitalize())

Output:


Hello world

⚠️ Point to Remember:

It only affects the first character. Useful for proper sentence formatting.


text = "hELLO PYTHON"
print(text.capitalize())

Output:


Hello python

📚 Practice Questions:


📚 Related Topics:


📌 Bookmark this blog or follow for updates!

Python Logo

👍 Liked this post? Share it with friends or leave a comment below!

Previous Post Next Post

Contact Form