In this post, you will learn about Python Strings, indexing in the string, concatenation of two strings, how to create a substring from a given string, multi-line String, Python Escape Characters, etc with a detailed explanation and example.
So let’s start from a very basic.
What is String in Python?
In general, the String is a sequence of characters but in python, you can say that anything (characters, numbers, symbols, etc) inside single or double quotes ( ‘ ___’ or “___” ) is considered a string.
Example: ‘Refer’ or “Noon”.
Create a String in Python
To create a String in python assign a variable with your string.
# Create a String
my_string = 'allinpython'
# my_string ---> is name of the variable.
print(my_string)
# or
my_string2 = "refer"
print(my_string2)
Output:
allinpython
refer
What is Indexing in String?
Indexing is the place value of each and every character of the string and it starts from 0 to len(string) - 1
.
Example:
With the help of index value, you can access every characters of the string.
my_string = 'REFER'
print(my_string[0])
print(my_string[1])
print(my_string[2])
print(my_string[3])
print(my_string[4])
Output:
R
E
F
E
R
String is immutable
A string is immutable because once a string is created you can not change or update it.
For Example:
my_string = 'refer'
my_string[0] = 'R'
print(my_string)
Output:
Traceback (most recent call last):
File "c:\Users\saurabh\Desktop\allinpython\String.py", line 2, in <module>
my_string[0] = 'R'
TypeError: 'str' object does not support item assignment
In the above example, we get an error while updating r --> R.
Concatenation of two strings
You can join (or concatenate) two strings with the help of ‘+’ operator.
name = 'roman'
surname = 'reigns'
full_name = name + surname
print(full_name)
Output:
romanreigns
Create a substring from a given String
With the help of Slicing you will create a substring from the given String.
my_string = 'james is so kind'
print('my string is : ', my_string)
# slicing will take starting and ending index values
# for Example: my_string[start : end]
my_sub_string = my_string[0:5]
print('my substring is : ',my_sub_string)
Output:
my string is : james is so kind
my substring is : james
Multi-line String in Python
For multi-line string you can use single quotes three times or double quotes three times (”’___ ”’ OR “””___”””) in python.
For Example:
my_string = '''this is demo of
multi-line string'''
print(my_string)
# OR
my_string2 = """\nthis is double quotes
example"""
print(my_string2)
Output:
this is demo of
multi-line string
this is double quotes
example
Python Escape Characters
There are many escape characters in python few are listed below:
- \n –> is use for new line
- \t –> is use for tab
- \\ –> is use for single backslash
- \b –>Backspace
- \” –> is use for double quote
print("hello\nworld")
print("hello\tworld")
print('hello\\world')
Output:
hello
world
hello world
hello\world
Check length of the String
You can use the len()
function to check the length of the string in python.
# length
# 1 2 3 4 5 6 7 8 9 10 11 12
# t h i s i s d e m o
my_string3 = 'this is demo'
print(len(my_string3))
Output:
12
Check substring is present or not
my_string5 = 'this is demo example for understanding'
# check if 'demo' present in my_string5 or not
if 'demo' in my_string5:
print('present')
else:
print('not present')
Output:
present
Check String is empty or not
name = ''
if name:
print('it is not empty')
else:
print('Empty')
Output:
Empty
Related Post:
String methods
Indexing, slicing & step arguments