What is String in Python | EP-22 String in Python | How to Manage String Data Type |Python Tutorials
What is a String in Python?
A String is a data structure in Python Programming that represents a sequence of characters. It is an immutable data type, meaning that once you have created a string, you cannot change it.
Syntax of String Data Type in Python
string_variable = 'Hello, world!'."""
How to Create a String in Python
Strings in Python can be created using single quotes or double quotes or even triple quotes.
Note: The triple quotes can be used to declare multiline strings in Python.
single_quote=('cybrosys')
double_quote =("cybrosys technologies")
triple_quote="""cybrosys technologies
pvt ltd"""
print(single_quote,"single")
print(double_quote,"double")
print(triple_quote,"triple")
Accessing characters in Python String
Individual characters of a String can be accessed by using the method of Indexing. Indexing allows negative address references to access characters from the back of the String, e.g. -1 refers to the last character, -2 refers to the second last character, & so on.
While accessing an index out of the range will cause an IndexError.
Only Integers are allowed to be passed as an index, float or other types that will cause a TypeError.
Python String Positive Indexing
company ="cybrosys"
print(company[2])
tutorial ="hi python"
print(tutorial[2],"blank")
Python String Negative Indexing
print(company[-3])
String Slicing Python
In Python Programming tutorials, the String Slicing method is used to access a range of characters
in the String. Slicing in a String is done by using a Slicing operator, i.e., a colon (:). One thing to keep in mind while using this method is that the string returned after slicing includes the character at the start index but not the character at the last index.
In this example, we will use the string-slicing method to extract a substring of the original string. The [3:7] indicates that the string slicing will start from the 3rd index of the string to the 7th index, (7th character not including). We can also use negative indexing in string slicing.
com ="cybrosys"
print(len(com))
print(com[3:7])
print(com[2:-2])
Python String Reversed
"""By accessing characters from a string, we can also reverse strings in Python Programming.
We can Reverse a string by using String slicing method."""
print(com[::-1])
BuildIn Reverse Function in Python
We can also reverse a string by using built-in join and reversed functions, and passing the string as the parameter to the reversed() function.
syntax:"".join(reversed(object))
tutorial1=" python tutorial"
t="".join(reversed(tutorial1))
print(t)
Deleting from a String
In Python, the 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. Although deletion of the entire String is possible with the use of a built-in del keyword. This is because Strings are immutable, hence elements of a String cannot be changed once assigned. Only new strings can be reassigned to the same name.
str1 ="python"
print(str1)
del str1[3]
Deleting Entire String
Deletion of the entire string is possible with the use of del keyword.
Further, if we try to print the string, this will produce an error because
the String is deleted and is unavailable to be printed.
syntax: del object
str1 ="python"
del str1
print(str1)
Updating a character
A character of a string can be updated in Python by first converting the string into a Python List and then updating the element in the list. As lists are mutable in nature, we can update the character and then convert the list back into the String.
Another method is using the string slicing method. Slice the string before the character you want to update, then add the new character and finally add the other part of the string again by string slicing.
syntax:"".join(object)
string1 ="cybrosys"
string2=list(string1)
string2[7]="p"
string3 ="".join(string2)
print(string2)
In the string-slicing method, we sliced the string up to the character we want to update, concatenated the new character, and finally concatenate the remaining part of the string.
string1 ="cybrosys"
slice =string1[0:2]+"p"+string1[3:]
print(slice)
Updating Entire String
As Python strings are immutable in nature, we cannot update the existing string. We can only assign a completely new value to the variable with the same name.
string1 ="cybrosys"
print(string1)
string1="python"
print(string1)
#PythonStrings #PythonTutorial #PythonForBeginners #LearnPython #Programming #StringDataType #PythonProgramming #PythonLearning #python #learntocode #codingtutorial #datascience
Connect With Us:
—————————————
➡️ Website: https://www.cybrosys.com/
➡️ Email: info@cybrosys.com
➡️ Twitter: / cybrosys
➡️ LinkedIn: / cybrosys
➡️ Facebook: / cybrosystechnologies
➡️ Instagram: / cybrosystech
➡️ Pinterest: / cybrosys