In this tutorial, you will learn how to wire, format, modify and delete strings in pythons. and also you will be introduced to various string operations and tasks.
The string is sequences of characters, using the syntax of either single or double or triple quotes.
The string is an array of bytes representing Unicode characters. However, Python does not have a character’s data type, a single character is simply a string with a length of 1 square bracket that can be used to access elements of the string.
Please read the related topic for more understanding of Python
Python Basic Syntax
Keywords and Identifiers
Variables and Data Types
Number in Python
Let’s the below example to create a string variable in python.
string1 = 'Hello world'
print(string1)
Output-
Hello world
It will print Hello world on screen. In this case, we are using ‘ (Single quote) to create a string in python.
string2 = "I'm a Software Engineer"
print(string2)
Output-
I'm a Software Engineer
It will print I’m a Software Engineer on screen. In this case, we are using ” (double quote) to create a string in python.
We can use a single quote inside the string while we use a double string.
string3 = '''Live
For
Live '''
print(string3)
It will print the below output on screen with formated. In this case, we are using ”’ or “”” (single/double quote three times) to create a string in python.
We can use a single quote and a double quote inside the string while we use a triple quote string.
Output-
Live For Live
Note:-
If you have single quotes in your string then use a double quote for declaration
If you need formated output string then use triple.
Contents
Accessing characters in Python
Python strings are ordered sequences it means we can using indexing and slicing to grab subsections of the string. Indexing notation use [] after the string (or variable assigned the string). Indexing allows you to grab a character from the string. These actions use [] square brackets and a number index to indicate positions of while you wish to grab.
Indexing: Indexing is used to obtain individual elements.
Slicing: Slicing is used to obtain a sequence of elements.
Indexing and Slicing can be done in Python Sequences types like list, string, tuple, range objects.
Indexing
Indexing starts from 0. Index 0 represents the first element in the sequence.
Python string Indexing allows negative address references to access characters from the block of the string
Negative indexing starts from -1. Index -1 represents the last element in the sequence.
Note –
While accessing an index out of the range will cause an index error
The only integer allows to be passed as an index, float or other types will cause a Type Error.
Slicing
Python string slicing allows you to grabs a subsection of multiple characters, a “Slice” of the string.
This has the following syntax
stringvariblename[start: stop: step]
Strat:- It is the numerical index for the slice start.
Stop:- It is the index you will go up to but not include.
Step:- It is the size of the jump you take.
S = "Hello World"
S[1:]
Output-
'ello World'
Delete/Updating from a String
In python string, updating or deletion of characters from a string is not allowed. This will cause an error because item assignment or item deletion from a string is not supported.
The string is immutable, Hence elements of string can not be changed once it has been assigned
Only new strings can be reassigned to the same name.
S = "Hello World"
S[2] = "N"
Type Error: str object does not support item assignment.
Output-
S = "Welcome to India"
Above the code used to reassign the entire string.
del S[3]
Type Error: str object does not support item deletion.
Output-
del S
It will delete the string variable from memory but if you again try to access this variable then you will get the below error
S
Name Error: name “S” is not defined.
Output-
Escape Sequencing:
While printing string with single and double quote in it cause SyntaxError because string already contains single or double quotes To print such a string eighter triple quote or escape sequence. The escape sequence starts with a backslash and can be interpreted differently.
string1 = '''I'm from "India" '''
string2 = 'I\'m from \"India\"'
print(string1)
print(string2)
Output-
I'm from "India" I'm from "India"
Other Escape Sequence in Python String:
\n : Insert a new line character in a string
\t : Insert a horizontal tab
\ : Insert a backslash character in a string
\” : insert a double quote
\’ : Insert a single quote
Ignoring a line break in a long string.
print('Welcome to \
Python')
Output-
Welcome to Python
\ : It split a long string over several lines by using \ continuation character as the last character on a line to ignore the break
Print the value of an expression
The calculation can be performed in the print statement
print('Sum is:', 9+6)
Output-
Sum is: 15
Repetition in Python String
Sometimes we need to repeat the string in the program, and we can do this easily by using the repetition operator in Python. The repetition operator is denoted by a ‘*’ symbol and is useful for repeating strings to a certain length.
letter ='Z'
print(letter * 5)
Output-
ZZZZZ
Concatenation Python String
Python string concatenation is the process of merging two or more strings. The + operator adds a string to another string. % lets you insert a string into another string value. Both operators are used to concatenate strings in Python.
string1 = 'Hello world '
string1 = string1 + 'concatenate me'
print(string1)
Output-
Hello world concatenate me
Triple Quoted Strings
IT begin and end with three double quote (“””) or three single quote(”’)
The multiline string allows inside triple quote string.
The string contains single or double-quotes.
Docstring, Which is the recommended way to document the purposes of certain program components.