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)
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 .
With the help of index value, you can access every character of the string.
my_string = 'REFER' print(my_string) print(my_string) print(my_string) print(my_string) print(my_string)
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.
my_string = 'refer' my_string = 'R' print(my_string)
Traceback (most recent call last): File "c:\Users\saurabh\Desktop\allinpython\String.py", line 2, in <module> my_string = '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)
Create a substring from a given String
Slicing helps you to create a substring from a given string.
my_string = 'james is so kind' print('my string is : ', my_string) # slicing take starting and ending index values # my_string[start : end] my_sub_string = my_string[0:5] print('my substring is : ',my_sub_string)
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.
my_string = '''this is demo of multi-line string''' print(my_string) # OR my_string2 = """\nthis is double quotes example""" print(my_string2)
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')
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))
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')
Check String is empty or not
name = '' if name: print('it is not empty') else: print('Empty')
Indexing, slicing & step arguments
Pingback: How to Reverse a Number in Python – allinpython.com