Python Strings šŸŽÆ

beginner
18 min

Python Strings šŸŽÆ

Welcome to the Python Strings tutorial! In this lesson, we'll explore everything you need to know about strings in Python. By the end of this tutorial, you'll have a solid understanding of strings, their properties, and how to manipulate them. Let's dive in! 🐠

What are Strings in Python? šŸ“

A string is a sequence of characters. In Python, strings are represented as objects of the str type. Here's an example of a string:

python
my_string = "Hello, World!"

Creating Strings šŸ’”

You can create strings using single quotes, double quotes, or triple quotes. Here are some examples:

python
single_quote_string = 'Hello' double_quote_string = "Hello" triple_quote_string = """Hello, World!"""

šŸ’” Pro Tip: Triple quotes allow you to create multi-line strings.

String Operations āœ…

Length of a String

To find the length of a string, use the len() function:

python
my_string = "Hello, World!" string_length = len(my_string) print(string_length) # Output: 13

Concatenation

To combine two strings, use the + operator:

python
string1 = "Hello" string2 = "World!" combined_string = string1 + " " + string2 print(combined_string) # Output: Hello World!

Indexing

To access characters in a string by their index, use square brackets []:

python
my_string = "Hello, World!" first_letter = my_string[0] print(first_letter) # Output: H

šŸ’” Pro Tip: Python uses zero-based indexing, which means the first item has an index of 0.

Slicing

To get a part of a string, use slicing:

python
my_string = "Hello, World!" part_of_string = my_string[0:5] print(part_of_string) # Output: Hello

In the above example, [0:5] means from the first index (0) to the fifth index (4), excluding the fifth index.

Repeating Strings

To repeat a string multiple times, use the * operator:

python
my_string = "Hello" repeated_string = my_string * 3 print(repeated_string) # Output: HelloHelloHello

String Methods šŸ’”

Python strings have several built-in methods that make it easy to manipulate them. Here are some examples:

Uppercase and Lowercase

To convert a string to uppercase or lowercase, use the upper() and lower() methods, respectively:

python
my_string = "Hello, World!" uppercase_string = my_string.upper() lowercase_string = my_string.lower() print(uppercase_string) # Output: HELLO, WORLD! print(lowercase_string) # Output: hello, world!

Finding Substrings

To find the position of a substring, use the find() method:

python
my_string = "Hello, World!" position_of_world = my_string.find("World") print(position_of_world) # Output: 6

Replacing Substrings

To replace a substring, use the replace() method:

python
my_string = "Hello, World!" replaced_string = my_string.replace("World", "Universe") print(replaced_string) # Output: Hello, Universe!

Quiz šŸ“

Quick Quiz
Question 1 of 1

What is the output of the following code?

That's it for our Python Strings tutorial! Now you have a good understanding of strings, their properties, and how to manipulate them. Happy coding! šŸš€