Python string: Based on python 3
Table of Contents
What are strings and characters in python?
In python 3, anything enclosed inside the quotation marks is a string. The string is an ordered sequence of UNICODE characters. The UNICODE is an ASCII (American Standard Code for Information Interchange) superset; There 143,859 UNICODE characters and only 128 ASCII characters.
A character is simply a string of size one.
Example:
>>> site = "www.bhutanpythoncoders.com"
>>> num = "12345"
>>> password = 'xyz2010'
In the above example; site, num, and password are variable and it holds string data types.
You can display a string literal with the print()
function:
>>> print("Hello world!")
Hello world!
>>> print("10/10/2020")
10/10/2020
String concatenation and repetition
Concatenation in the context of programming is the operation of joining two strings together. The term”concatenation” literally means to merge two things (src: techopedia).
You can concatenate strings with a plus (+) operator.
Example 1:
>>> fname = "Sonam"
>>> lname = "Tobgay "
>>> name = fname + lname
>>> print(name)
Sonam Tobgay
Example 2:
>>> num1 = "50"
>>> num2 = "60"
>>> num = num1 + num2
>>> print(num)
5060
#The output is not 110 because num1 and num2 stores string. So, rather then adding it, it will merge num1 and num2.
Strings can be repeated with the * sign.
Example 1:
>>> fname = "Sonam"
>>> lname = "Tobgay "
>>> name = fname + lname
>>> print(name * 3)
SonamTobgay SonamTobgay SonamTobgay
Example 2:
>>> print("Hello " * 5)
Hello Hello Hello Hello Hello
String indexing
Python support positive and negative indexing. Positive indexing starts from zero and it goes till n number of characters in the string. Negative indexing starts from -1 and it goes till -n number of characters in the string. Negative indices start from -1 because -0 is the same as 0.
A character is simply a string of size one.
Square bracket [ ] is used to access the character or element of the string;
Example:
>>> string = "BHUTAN"
>>> print(string[1]) # Character in position 1
H
>>> print(string[-2]) # Character in position -2
A
>>> print(string [-5]) # Character in position -5
H
>>> print(string[5])
N
Attempting to use an index that is out of bounds will result in an error;
>>> string = "BHUTAN"
>>> print(string[10])
Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module>
print(string[10])
IndexError: string index out of range
String slicing
In addition to string indexing, string slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain substring;
Example:
>>> string = "BHUTAN"
>>> print(string[0:2]) # Positive Indexing
BH
>>> print(string[-3:-1]) # Negative Indexing
TA
>>> print(string[3:-1]) # Mixed Indexing
TA
Notes:
- The start index is always included, and the end is always excluded. That’s why string[0:2] printed BH; B is at index 0, H at index 1, and U at index 2 but excluded being the end index.
- You can use both the negative and positive index together.
The length of a slice is the difference of the indices if both are within bounds. For example, the length of the string[1:3] is 2.
Example:
>>> string = "BHUTAN"
>>> print(string[:2]) # Character from the beginning to index 2 (excluded)
BH
>>> print(string[4:]) # Character from index 4(included) till end
AN
>>> print(string[-2:]) # Charcter from second last till end
AN
Out of range slice indexes are handled gracefully when used for slicing;
Example:
>>> string = "BHUTAN"
>>> print(string[2:100])
UTAN
>>> print(string[100:]) # Empty string will be generated. Not an error.
String slicing takes the third optional argument which represents a step value.
Example:
>>> string = "BHUTAN"
>>> print(string[1:5:2]) # Starts at index 1 and ends at index 5(excluded) in the step on 2
HT
>>> print(string[::1]) # Printing all the characters in string in step of 1
BHUTAN
Printing the string in reverse order;
>>> string = "BHUTAN"
>>> print(string[::-1])
NATUHB
String is immutable
String objects are immutable; the content of the String object can’t be changed, once it is created.
For example:
>>> var = "food"
>>> var[0] = "g"
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
var[0] = "g"
TypeError: 'str' object does not support item assignment
finding length of the string
The built-in function len() returns the length of a string;
Example:
>>> string = "BHUTAN"
>>> print(len(string)) # There are 6 characters in string
6
Checking character in the string
To check whether a character or substring is present in the main string, we use in and not in keyword; If the character is found in the string it will return True otherwise False.
Example:
>>> string = "BHUTAN"
>>> print("B" in string)
True
>>> print("B" not in string)
False
>>> print("F" in string)
False
>>> print("TAN" not in string)
False
Raw-string
Raw-string in python suppresses the actual meaning of escape characters. It is represented by the letter ‘r’. It can be lower case or upper case but must be placed immediately preceding the first quote mark.
Example:
# Without using raw string
>>> print ("c:\\user\\Dawa")
c:\user\Dawa
# Using raw string
>>> print (r"c:\\user\\Dawa")
c:\\user\\Dawa
Deleting a string
To delete the string you have to use the keyword del. If you try to access a string after deleting it, it will give you a NameError.
Example:
>>> string = "BHUTAN"
>>> print(string)
BHUTAN
>>> del string # Deleting string
>>> print(string)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
print(string)
NameError: name 'string' is not defined
There are four ways to format the output string. I have explained about all four-way here.