- A Python string is a collection of characters enclosed by single, double, or triple quotes.
- The string is an immutable sequence data type.
- The computer does not understand the characters; instead, it stores the manipulated character as a combination of 0’s and 1’s internally.
- Each character is encoded in either ASCII or Unicode. As a result, Python strings can also be referred to as a collection of Unicode characters.
- Python doesn’t support the character data type; instead, a single character written as ‘p’ is treated as the string of length 1.
Creating a String :
We can make a string by enclosing the characters in single or double quotation marks. Python also supports triple quotes to represent strings, but they are typically used for multiline strings or docstrings.
str1='This is a string in Python' print(str1) str2="""This is the Multi-line string.""" print(str2) str3='Welcome to "Python Tutorial" on Coderzpy.com' print(str3)
This is a string in Python This is the Multi-line string.Welcome to "Python Tutorial" on Coderzpy.com
Individual characters of a String can be accessed in Python using the Indexing method. Indexing enables negative address references to access characters from the String’s back end, for example, -1 refers to the last character, -2 refers to the second last character, and so on.
The slicing method is used to access a range of characters in the String. A Slicing operator is used to slice a String (colon).
string_name[starting_index : finishing_index]
- String_name is the name of the variable holding the string.
- starting_index is the index of the beginning character which you want in your sub-string.
- finishing_index is one more than the index of the last character that you want in your substring.
# Given String str = "CHARACTERS" # Start Oth index to end print(str[0:]) # Starts 1th index to 4th index print(str[1:5]) # Starts 2nd index to 3rd index print(str[2:4]) # Starts 0th to 2nd index print(str[:3]) #Starts 4th to 6th index print(str[4:7])
CHARACTERS HARA AR CHA ACT
Note: In Python, the Update or deletion of characters from a String is not allowed.
Deleting the Entire String:
The del keyword can be used to delete the entire string. Furthermore, attempting to print the string will result in an error because String has been deleted and is no longer available for printing.
String1 = "CHaracters" print("Initial String: ") print(String1) # Deleting a String # with the use of del del String1 print("\nDeleting entire String: ") print(String1)
Initial String: CHaracters Deleting entire String: Traceback (most recent call last): File "<string>", line 10, in <module> NameError: name 'String1' is not defined
Arithmetic operators, do not operate on strings. There are, however, string processing operators.
|+||Appends the second string to the first||>>> a=’hello’|
|*||Concatenates multiple copies of the same string||>>> a=’hello’|
|||Returns the character at the given index||>>> a = ‘Python’|
|[ : ]||Fetches the characters in the range specified by two index operands separated by the : symbol||>>> a = ‘Python’|
|in||Returns true if a character exists in the given string||>>> a = ‘Python’|
>>> ‘x’ in a
>>> ‘y’ in a
>>> ‘p’ in a
|not in||Returns true if a character does not exist in the given string||>>> a = ‘Python’|
>>> ‘x’ not in a
>>> ‘y’ not in a
The escape character is used to call up a different implementation of the next character in a sequence. Backslash \ is used as an escape character in Python. To insert a character into a string, use a backslash character followed by the character you want to insert.
The following table lists escape sequences in Python.
|\nnn||Octal notation, where n is in the range 0-7||>>> ‘\101’|
|\xnn||Hexadecimal notation, where n is in the range 0-9, a-f, or A-F||>>> ‘\x48\x69’|
|\onn||Octal notation, where n is in the range 0-9||>>> “\110\151”|
Please follow me to read my latest post on programming and technology if you like my post.
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.