Python_Tutorial #Class-04 | Strings | Learn Python

In today's class, we'll learn about strings. There's a lot we can do with strings in Python and that too in an easier way. Every section of this class has some practical examples along with the compiler output.

Let's dive into today's session.

Topics we'll be covering today:
    1. Creating Strings
    2. Printing Strings
    3. String Indexing and Slicing
    4. String Properties
    5. String Methods

1. Creating Strings

We learned in the variable section how to create a variable, similarly, we'll be creating a  variable of type String. The syntax is variable_name = String_value.


# Single Text

text = "Python"

# Entire phrase
text_1 = "I love Python"


Now let's learn about printing Strings.

2. Printing Strings

To display a string on the output terminal use print function.

# print( ) function prints given value on output screen.



I love Python.
This will print output on a new line
Or you can use
to change line.
Hello,
World!

3. Indexing and Slicing

We know String is collections of characters and numbers. Every character has an index number that can be used to point each character of a string individually. To access characters by index number we use brackets [ ].  Also, know that the index number in Python starts with 0.

>>>text = 'I LOVE PYTHON'


# We will print character at 0 index, which is the first character

>>>print(text[0] )
>>>print(text[5] )
I
E


Length Of String
We can use Python built-in function len() to know the length of the string.

>>>print(len(text))
13


Slicing
We can use : to perform slicing which will cut-out a substring from the original string. The syntax to slice a string is  variable_name[starting_index : ending_index ] (last index is excluded.)



PYTHON
LOVE PYTHON
I LOVE

4. String Properties

The most important property of string is Immutability. This means elements of string cannot be changed or replaced once created.


# creating a string variable
>>>author_name =  "Neeraj"
# trying to change one character of string
>>>author_name[1] = 'E'
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    text[16] = 'U'
TypeError: 'str' object does not support item assignment

Trying to change the character of the string, we get an error. Either we can reinitialize the variable or concatenate it. Concatenation means joining two strings together as one. To concatenate strings, use + sign between them.

>>>print(author_name + ' Amoli')
Neeraj Amoli

5. String Methods

We'll be discussing some basic inbuilt string functions.
Objects in Python usually have built-in methods. These methods are functions inside the object (we will learn about these in much more depth later) that can perform actions or commands on the object itself.

We call methods with a period and then the method name. Methods are in the form:
object.method_name(parameters)

Where parameters are extra arguments we can pass into the method. Don't worry if the details don't make 100% sense right now. Later on, we will be creating our own objects and functions!

Here are some examples of built-in methods in strings:


With this, I wrap up today's class, hope you learned something new today. You can practice what you have just learned in the Python IDE below.


Thank You

Drop your queries in the comment box or mail me at [email protected]

Post a Comment

0 Comments