Python String
Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.
- Python3
Output:
Computer Science portal
Creating a String in Python
Strings in Python can be created using single quotes or double quotes or even triple quotes.
- Python3
Output:
String with the use of Single Quotes: Welcome to the World String with the use of Double Quotes: I'm a String with the use of Triple Quotes: I'm a Geek and I live in a world of "Geeks" Creating a multiline String: For LifeAccessing characters in Python String
In Python, 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, and 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.
- Python3
Output:
Initial String: ujjwal First character of String is: u Last character of String is: lReversing a Python String
With Accessing Characters from a string, we can also reverse them. We can Reverse a string by writing [::-1] and the string will be reversed.
- Python3
Output:
manWe can also reverse a string by using built-in join and reversed function.
- Python3
Output:
ujjwalString Slicing
To access a range of characters in the String, the method of slicing is used. Slicing in a String is done by using a Slicing operator (colon).
- Python3
Output:
Initial String: GeeksForGeek Slicing characters from 3-12: Slicing characters between 3rd and 2nd last character:Deleting/Updating from a String
In Python, Updation 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. 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 it has been assigned. Only new strings can be reassigned to the same name.
Updation of a character:
- Python3
Error:
Traceback (most recent call last):
File “/home/360bb1830c83a918fc78aa8979195653.py”, line 10, in
String1[2] = ‘p’
TypeError: ‘str’ object does not support item assignmentUpdating Entire String:
- Python3
Output:
Initial String: Hello, I'm a Updated String: Welcome to the WorldDeletion of a character:
- Python3
Error:
Initial String:
Hello, I’m a GeekDeleting character at 2nd Index:
Helo, I’m a GeekDeleting 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 String is deleted and is unavailable to be printed.
- Python3
Error:
Traceback (most recent call last):
File “/home/e4b8f2170f140da99d2fe57d9d8c6a94.py”, line 12, in
print(String1)
NameError: name ‘String1’ is not definedEscape Sequencing in Python
While printing Strings with single and double quotes in it causes SyntaxError because String already contains Single and Double Quotes and hence cannot be printed with the use of either of these. Hence, to print such a String either Triple Quotes are used or Escape sequences are used to print such Strings.
Escape sequences start with a backslash and can be interpreted differently. If single quotes are used to represent a string, then all the single quotes present in the string must be escaped and same is done for Double Quotes.
- Python3
Output:
Initial String with use of Triple Quotes: I'm a Escaping Single Quote: I'm a Escaping Double Quotes: I'm a Escaping Backslashes: C:\Python\Geeks\ Tab: Hi New Line: PythonTo ignore the escape sequences in a String, r or R is used, this implies that the string is a raw string and escape sequences inside it are to be ignored.
- Python3
Output:
Printing in Octal with the use of Escape Sequences: Hello Printing Raw String in Octal Format: This is \110\145\154\154\157 Printing in HEX with the use of Escape Sequences: This is Geeks in HEX Printing Raw String in HEX Format: This is \x47\x65\x65\x6b\x73 in \x48\x45\x58Formatting of Strings
Strings in Python can be formatted with the use of format() method which is a very versatile and powerful tool for formatting Strings. Format method in String contains curly braces {} as placeholders which can hold arguments according to position or keyword to specify the order.
- Python3
Output:
Print String in default order: shyam Print String in Positional order: ram Print String in order of Keywords: ujjwalIntegers such as Binary, hexadecimal, etc., and floats can be rounded or displayed in the exponent form with the use of format specifiers.
- Python3
Output:
Binary representation of 16 is 10000 Exponent representation of 165.6458 is 1.656458e+02 one-sixth is : 0.17A string can be left() or center(^) justified with the use of format specifiers, separated by a colon(:).
- Python3
Output:
Left, center and right alignment with Formatting: ram ram ram ram was founded in 2009 !Old style formatting was done without the use of format method by using % operator
- Python3
Output:
Formatting in 3.2f format: The value of Integer1 is 12.35 Formatting in 3.4f format: The value of Integer1 is 12.3457