Python String Tutorial for Beginners
Last updated Feb 01, 2021In this tutorial, we are going to learn about the strings in python, their uses, and how to use them in python. We are going to know everything in brief with examples in this tutorial. Also, we will know about the various string operations and functions in this section. So let's get started with what is a string?
What is a String?
A string is usually a character in the sequence. In simple words, it is just a symbol with characters. For example, the English language consists of 26 characters.
When we have to define any name, address, etc that contains characters. We have to use the strings in python.
Why String is Used?
The computer only deals with binary language, they do not deal with characters. The string is used to make the computer understand that an input value consists of characters. Due to which you are able to see characters on your screen but internally the characters are stored in a combination of 0 and 1 only. This character to number conversion is known as encoding.
When you recall the string value then it is again converted from binary to characters and display on-screen which is known as decoding.
In short, if you want to define any characters in python programming then you have to use "String" so that the computer can understand that it is a character.
Syntax to use a string in python
There is a proper syntax to use the string in python. You must have to use this syntax to initialize your character to a variable or to print the string. Here is the syntax-
-
Assign String to any variable
a = "Hello"
|
Here "Hello" is a string and "a" is a variable where we have assigned the string.
The string should be followed by single or double quotation marks. For example "Python" which means python is our string. Now let's see how to print the above string on the screen.
Print the Variable
a = "Hello"
print(a)
|
Output:
Hello
Here we have used the print() function to display the value of variable "a" that is "Hello". This code will print the value of 'a' on the screen.
Print the string
If we have to directly print the string without initializing the code. Here is the code
print("Hello") |
Output:
Hello
Here we have used the quotation marks in Hello because we are directly printing the string value without storing it in any variable. If we first store the value in any variable then we have to insert the variable inside the print() function without quotation marks.(Shown in the above example)
Multiline Strings
If you want to store multiline strings to a variable then you have to use three double quotes or three single quotes. For example-
- By using 3 double quotes
- By using 3 single quotes
By using 3 double quotes
a = """Python is an easy programming language,
you can easily learn python within few weeks,
Programmers are loving python"""
print(a)
|
Output:
Python is an easy programming language,
you can easily learn python within few weeks,
Programmers are loving python
By using 3 single quotes
a = '''Python is an easy programming language,
you can easily learn python within few weeks,
Programmers are loving python'''
print(a)
|
Output:
Python is an easy programming language,
you can easily learn python within few weeks,
Programmers are loving python
Strings as Arrays
In python, the string represents Unicode characters like other programming languages. This means that we can easily find or print any element of the string by using this property. Here is the code to explain you briefly
a = "Hello, Python!"
print(a[2])
|
Output:
l
In the above code, we have used the square brackets to access the 2nd element in the string (Keep in mind that the first character of the string is positioned 0). The following code will print 'l' which is in the 2nd position of the string.
Key points
-
Make sure to use square brackets to get any element from the string.
-
You should know the position of the first character in the string is 0.
Using string in Loops
Now as we know that strings are arrays that means we can easily use them in for loop. To use For loops you should go through this tutorial Python For Loops. Here is the code to justify our statement
for b in "apple":
print(b)
|
Output:
a
p
p
l
e
Check Length of String
We use len() function to get length of string. Here is the code to explain in brief
a = "Hello Python!"
print(len(a))
|
Output:
14
How to create a string in Python
Now as we know about all the syntax to use the strings in the python. We are ready to create our string in python. Let's get started
# Initializing our strings in Python
first_string = 'Hello'
# Print the value of variable
print(first_string)
# Using double quotation marks
first_string = "Hello"
# Print the value of variable
print(first_string)
# Using Multiline Strings
first_string = '''Hello,
Python,
Programmers'''
# Print the value of variable
print(first_string)
# Using for loop
for x in "Python":
print(x)
|
When you will run this code your output will be:
Hello
Hello
Hello,
Python,
Programmers
P
y
t
h
o
n
How to change or delete String
Strings are immutable which means that the string elements cannot be changed after getting assigned. To change its value we simply have to reassign the different value in the same variable
a = 'Hello Python'
print(a)
a= 'Python World'
print(a)
|
Output:
Hello Python
Python World
When you will run this program you will notice that the old value of variable a is 'Hello Python' but after reassigning the another value in the same variable the new value of a is 'Python World'. This is how we can change the value of string in python.
To Delete:
You cannot delete or remove any elements from the string. If you want to delete the whole string then you have to use the del keyword
String related Examples
String index out of range error in python
How remove last character from a string in python
Article Contributed By :
|
|
|
|
939 Views |