Welcome to the Python Chronicles! In this lesson, we will learn about strings in Python. Strings are used to represent text that are an essential part of any programming language. Understanding how to work with strings is special for manipulating and processing textual data. In the end of this lesson, you will have a good understanding about strings and learn various techniques to work with them effectively. So, let's get started!

Working with Strings


What are Strings?

In Python, a string is a sequence of characters written within single quotes ('') or double quotes (""). Strings can contain letters, numbers, symbols, and spaces. They are used to represent and manipulate text-based data.


what are strings

In this example, we created a string variable named "message" and assigned the value "Hello, Python!" to it.


String Operations

Python provides various operations and functions to edit and configure strings. Let's explore some string operations.


1.Concatenation: Combining Strings

We can concatenate strings using the "+" operator. It joins two or more strings together.


concatenate

In the example above, we concatenated the first_name, a space, and the last_name to create the full_name string. So the output will be like this "John Doe".


2.String Length: Counting Characters

We can get the the length of a string using the len() function. It returns the number of characters in a string.


len() func

In this example above, we calculated the length of a string and assigned it to the length variable. So the output will be like this 14.


3.Accessing Characters: Indexing and Slicing

We can access the characters within a string using indexing. In Python, indexing starts from 0.


indexing


In the example above, we accessed the first character of the message string using index 0.

We can also extract some of  strings using slicing. Slicing allows us to create a new string by specifying a range of indices.


slicing

In the example above, we extracted the "Hello" from the message string using slicing.


String Methods

Python provides various kind of built-in string methods that allow us to perform various operations on strings. Let's learn some commonly used string methods.


1.upper() and lower(): Changing Case

The upper() method converts all characters in a string to uppercase (Write all in capital letters), while the lower() method converts them to lowercase (Write all in simple letters).


changing case

In the example above, we converted the message string to uppercase and lowercase, respectively. After that converting complete the changed values are like this;

    • uppercase_message = "HELLO, PYTHON!"
    • lowercase_message = "hello, python"

2.strip(): Removing Whitespace
The strip() method removes any leading or trailing whitespace from a string.

strip()

In the example above, we removed the leading and trailing whitespace from the message string using the strip() method. The trimmed_message will like this;
    • trimmed_message = "Hello, Python!"

3.split(): Splitting a String
The split() method splits a string into a list of substrings based on a specified delimiter.

split()

In the example above, we split the message string into individual words using the split() method. The words variable will be like this;
    • words = ['Hello,' 'Python!']

Conclusion

In this session, we looked at the Python strings. Strings are used to represent text-based data and may be changed using a various operations and methods. We learned how to concatenate strings, determine string length, access individual characters via indexing and slicing, and use built-in string functions to run actions such as changing case, deleting whitespace, and dividing strings. Understanding strings and how to manipulate them is very useful when working with textual data in Python. Continue to practice and explore with strings to improve your understanding. Happy coding!

Post a Comment

Previous Post Next Post