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.
For instance,
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)
Output:
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.
Example:
str=”Coderzpy”
Characters | C | o | d | e | r | z | p | y |
Forward Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
Backward Index | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
The slicing method is used to access a range of characters in the String. A Slicing operator is used to slice a String (colon).
Syntax:
string_name[starting_index : finishing_index]
Example:
# 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])
Output:
CHARACTERS
HARA
AR
CHA
ACT
Note: In Python, the Update or deletion of characters from a String is not allowed.
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.
For instance,
String1 = "CHaracters"
print("Initial String: ")
print(String1)
# Deleting a String
# with the use of del
del String1
print("\nDeleting entire String: ")
print(String1)
Output:
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.
Operator | Description | Example |
---|---|---|
+ | Appends the second string to the first | >>> a=’hello’ >>> b=’world’ >>> a+b ‘helloworld’ |
* | Concatenates multiple copies of the same string | >>> a=’hello’ >>> a*3 ‘hellohellohello’ |
[] | Returns the character at the given index | >>> a = ‘Python’ >>> a[2] t |
[ : ] | Fetches the characters in the range specified by two index operands separated by the : symbol | >>> a = ‘Python’ >>> a[0:2] ‘Py’ |
in | Returns true if a character exists in the given string | >>> a = ‘Python’ >>> ‘x’ in a False >>> ‘y’ in a True >>> ‘p’ in a False |
not in | Returns true if a character does not exist in the given string | >>> a = ‘Python’ >>> ‘x’ not in a True >>> ‘y’ not in a False |
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.
Escape sequence | Description | Example |
---|---|---|
\\ | Backslash | >>> “Hello\\Hi” Hello\Hi |
\b | Backspace | >>> “ab\bc” ac |
\f | Form feed | |
\n | Newline | >>> “hello\nworld” Hello world |
\nnn | Octal notation, where n is in the range 0-7 | >>> ‘\101’ A |
\t | Tab | >>> ‘Hello\tPython’ Hello Python |
\xnn | Hexadecimal notation, where n is in the range 0-9, a-f, or A-F | >>> ‘\x48\x69’ Hi |
\onn | Octal notation, where n is in the range 0-9 | >>> “\110\151” Hi |
Note: also read about Armstrong Number in C++Data Types in Python
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…