1. How to Create String in Python
A string is a sequence of characters. A character is simply a symbol displayed on the screen using pixels. The characters displayed on the screen are stored by binary digits 0 and 1.
The process of converting characters to binary numbers is called encoding. And the reverse process is called decoding. ASCII and Unicode are widely used encoding schemes.
A string in Python consists of a sequence of Unicode characters. The Unicode encoding scheme can encode and decode all characters in all languages, special characters, etc.
Creating Strings in Python
Strings in Python can be created using single quotes, double quotes, and even triple quotes.
# Creating a String with single Quotes
my_string = 'Gochocit.com'
print(my_string)
# Creating a String with double Quotes
my_string = "Gochocit.com"
print(my_string)
# Creating a String with triple Quotes
my_string = '''Gochocit.com'''
print(my_string)
# Creating a String with triple Quotes
my_string = """Hello, welcome to Gochocit.com!"""
print(my_string)
# Creating String with triple Quotes allows multiple lines
my_string = '''Hello,
This
is
Gochocit.com'''
print(my_string)
Result
Gochocit.com
Gochocit.com
Gochocit.com
Hello, welcome to Gochocit.com!
Hello,
This
is
Gochocit.com
Strings referenced by variables
We can assign a string to a variable using the assignment operator (=). The variable will then reference an object of the str class. Example:
website = "Gochocit.com"
print(website)
print(type(website))
Result
Gochocit.com
<class 'str'>
2. Accessing characters of a String in Python
We can access each character in a String by its index. The index of a String starts at 0 and must be an integer. Python permits the use of negative integer indices for Strings. An index of -1 refers to the position of the last character on the right side of the String.
website = 'gochocit.com'
print('website = ', website)
#first character
print('website[0] = ', website[0])
#character at index 5
print('website[5] = ', website[5])
#last character
print('website[-1] = ', website[-1])
#character at index -5
print('website[-5] = ', website[-5])
Result
website = gochocit.com
website[0] = g
website[5] = c
website[-1] = m
website[-5] = t
We can access a range of consecutive characters by using the slicing operator (:).
website = 'gochocit.com'
print('website = ', website)
#slicing 2nd to 5th character
print('website[1:5] = ', website[1:5])
#slicing 6th to 2nd last character
print('website[5:-2] = ', website[5:-2])
Result
website = gochocit.com
website[1:5] = ocho
website[5:-2] = cit.c
If you try to access a character in a String using an index that is outside the range of valid indexes, it will result in an IndexError. If the index is not of type integer, it will result in a TypeError.
website = 'gochocit.com'
print('website = ', website)
# index must be in range
# IndexError: string index out of range
print('website[50] = ', website[50])
# index must be an integer
# TypeError: string indices must be integers
print('website[1.5] = ', website[1.5])
3. String Operations in Python
3.1. Modifying or deleting a String
In Python, Strings have immutable values. This means that the characters within a String cannot be changed. If we attempt to modify a String, a TypeError will be thrown.
website = 'gochocit.com'
website[1] = 'a'
Result
TypeError: 'str' object does not support item assignment
If we want a different String, we can simply assign that String to a variable.
website = 'gochocit.com'
print(website)
website = "lhvinhckc.info"
print(website)
Result
gochocit.com
lhvinhckc.info
We cannot delete a character in a String, but we can use the del keyword to delete a String.
website = 'gochocit.com'
#TypeError: 'str' object doesn't support item deletion
del website[1]
del website
#NameError: name 'website' is not defined
print(website)
3.2. Operators for manipulating strings
The + operator is used to concatenate two or more strings. The * operator is used to repeat a string several times.
# Python String Operations
str1 = 'Hello, '
str2 ='Welcome to '
str3 ='Gochocit.com!'
# using +
print('str1 + str2 + str3 = ', str1 + str2 + str3)
# using *
print('str3 * 3 =', str3 * 3)
Output
str1 + str2 + str3 = Hello, Welcome to Gochocit.com!
str3 * 3 = Gochocit.com!Gochocit.com!Gochocit.com!
3.3. Iterating through a String
We can iterate through each character in a string using a for loop. Below is an example that counts the number of ‘c’ characters in a string.
# Iterating through a string
count = 0
for letter in 'Gochocit.com':
if(letter == 'c'):
count += 1
print(count,'letters found')
Output
3 letters found
3.4. Check if a String belongs to another String
Use the keyword in or not in to check if a String belongs to another String.
website = 'gochocit.com'
#True
print("it" in website)
#False
print("ckc" in website)
#True
print("itckc" not in website)
#False
print("com" not in website)
3.5. Formatting Strings in Python
Python provides the ability to format strings using the format() function. The format() function uses curly braces {} to hold space in the String for the arguments of the format() function.
# Default order
String1 = "{} {} {}".format('Goc', 'hoc', 'it')
print("Print String in default order: ")
print(String1)
# Positional Formatting
String1 = "{1} {0} {2}".format('Goc', 'hoc', 'it')
print("Print String in Positional order: ")
print(String1)
# Keyword Formatting
String1 = "{l} {h} {g}".format(g='Goc', h='hoc', l='it')
print("Print String in order of Keywords: ")
print(String1)
Result
Goc hoc it
Print String in Positional order:
hoc Goc it
Print String in order of Keywords:
it hoc Goc
We can format integer and float numbers with special format symbols. Example:
# Formatting of Integers
String1 = "{0:b}".format(16)
print("Binary representation of 16 is ")
print(String1)
# Formatting of Floats
String1 = "{0:e}".format(165.6458)
print("Exponent representation of 165.6458 is ")
print(String1)
# Rounding off Integers
String1 = "{0:.2f}".format(1/6)
print("one-sixth is : ")
print(String1)
Result
Binary representation of 16 is
10000
Exponent representation of 165.6458 is
1.656458e+02
one-sixth is :
0.17
If not using the format() function, we can use the formatting type with the % operator. Example:
num1 = 12.3456789
print("Formatting in 3.2f format: ")
print('The value of num1 is %3.2f' % num1)
print("Formatting in 3.4f format: ")
print('The value of num1 is %3.4f' % num1)
Result
Formatting in 3.2f format:
The value of num1 is 12.35
Formatting in 3.4f format:
The value of num1 is 12.3457
3.6. Escape characters in Python
When printing strings with single or double quotes or an invalid character, an error will occur. For example:
#SyntaxError: unterminated string literal
print("He said, "What's there?"")
txt = "We are the so-called "Vikings" from the north."
# SyntaxError: invalid syntax
print(txt)
#SyntaxError: unterminated string literal
print("C:\Python\")
We solve these cases by using escape character \. In addition, we can ignore the escape character by placing r or R before the String.
my_string = 'I\'m a "man"'
print("Escaping Single Quote: ")
print(my_string)
my_string = "I'm a \"man\""
print("Escaping Double Quotes: ")
print(my_string)
my_path = "C:\\Python\\Geeks\\"
print("Escaping Backslashes: ")
print(my_path)
print("This is \x48\x45\x58 representation")
#ignore escape character
print(r"This is \x48\x45\x58 representation")
Result
Escaping Single Quote:
I'm a "man"
Escaping Double Quotes:
I'm a "man"
Escaping Backslashes:
C:\Python\Geeks\
This is HEX representation
This is \x48\x45\x58 representation
You can see all the escape characters here.
3.7. Some commonly used functions when working with Strings in Python
Function | Description |
---|---|
enumerate() | Returns an object including all the index and corresponding characters in the String |
len() | Returns the number of characters in the String |
lower() | Converts characters in the String to lowercase |
upper() | Converts characters in the String to uppercase |
join() | Joins multiple Strings together |
split() | Returns a list of words in the String |
find() | Returns the position of the first occurrence of a string within another string. |
replace() | Replaces a sub-String with another String in the parent String. Then returns the new String. |
website = "Welcome to Gochocit.com"
# enumerate()
list_enumerate = list(enumerate(website))
print('list(enumerate(website) = ', list_enumerate)
#character count
print('len(website) = ', len(website))
print(website.lower())
print(website.upper())
print(website.split())
print(website.join(["How ", " are ", " you?"]))
print(website.find("it"))
print(website.replace('Gochocit.com','lhvinhckc.info'))
Result
list(enumerate(website) = [(0, 'W'), (1, 'e'), (2, 'l'), (3, 'c'), (4, 'o'), (5, 'm'), (6, 'e'), (7, ' '), (8, 't'), (9,
'o'), (10, ' '), (11, 'G'), (12, 'o'), (13, 'c'), (14, 'h'), (15, 'o'), (16, 'c'), (17, 'i'), (18, 't'), (19, '.'), (20, 'c'), (21, 'o'), (22, 'm')]
len(website) = 23
welcome to gochocit.com
WELCOME TO GOCHOCIT.COM
['Welcome', 'to', 'Gochocit.com']
How Welcome to Gochocit.com are Welcome to Gochocit.com you?
17
Welcome to lhvinhckc.info