A sequence of characters or English alphabets is treated as a string. It can be a symbol like 26 alphabets. Computers don't understand these characters. The system treats all characters as a binary number. Hence every component is treated as a binary form like 0 or 1
. The conversion between number to character and character to number depends on the ASCII value. In Python, a string is demonstrated as Unicode characters. Strings are created by using single or double-quotes
. String creation is the same as creating value to a variable. The example is given below,
print("Hello")
print('Python')
It is similar to other programming languages. We have to write the variable name on the left-hand side of the equal to sign (=
) and write the string value in the right-hand side of the equal to sign using quotation marks. An example is given below,
a = 'Hello'
b = "Python"
print(a)
print(b)
Python has a concept of sub-string that can extract some alphabets or numbers from the main string. For access, we have to use the square brackets for slicing and index to get the sub-string. For example,
a = 'Hello Python'
b = "Object-oriented Programming"
print ("a[0]: ", a[0]) # this will print H
print ("a[2:4]: ", a[2:4]) # this will print je
We can assign a multiline string to a variable in Python. To get this we have to use three single or double-quotes. For example,
line = """the quick brown
fox jumps over
the lazy dog"""
print(line)
In Python, we can update a string variable by reassigning a variable to another string. An example is given below,
a = 'Hello World'
print ("New String is :- ", a[:6] + 'Python') #it will print Hello Python
Python does not have any character data type. It is calculated by its length. Strings in Python are arrays of bytes that represent Unicode characters. To access the elements of the string square bracket is used. An example is given below,
s= "This is Python Tutorial"
print(s[1]) #this will print h
Python support both the positive and negation indexing. We have already learned Positive indexing. The same rule will be used in negative indexing for calculation but it will start from the end of the string. Hence, a negative sign will come before the index number. For example,
index = "Hello, Python"
print(index[-10:-2])
#this will print lo, Pyth
Python has a built-in function called len(). This function is used to calculate the length of the string. For example,
length = "Hello, Python!"
print(len(length))
#this len function will return 14
Python has various built-in methods. These methods are widely used for text formatting. Data cleaning and Data processing is an important task and that can be done Python. Python methods are,
Strip()
method: It is used to remove unnecessary space from the beginning and the end of a text.
m = " Hello, Python "
print(m.strip()) # this will return Hello, Python
lower()
method: It is used to convert the entire text into lower case.
l = "Hello, Python"
print(l.lower()) # this will return hello, python
upper()
method: It is used to convert the entire text into upper case.
u = "Hello, Python"
print(u.upper()) # this will return HELLO, PYTHON
replace()
method: It is used to convert from one text to another.
r = "Hello, Python"
print(r.replace("o","w" )) # this will return Hellw, Pythwn
split()
method: It is used to divide the string into various parts as per the delimiter or separator used.
sp = "Hello, Python"
print(sp.split(",")) # this will return ['Hello', 'Python']
The string format()
class in the string module allows programmers to create and customize their own string formatting behaviors using the built-in methods. The String format() is an in-built public method of Formatter class to perform a string formatting operation.
For details string format tutorial, please go to Python String Format.