13 Nov Strings in Python
Introduction
A string is a sequence of characters. Computers deals with numbers (binary), even though we see characters, internally it is stored and manipulated as a combination of 0’s and 1’s. ASCII and Unicode are some of the popular encoding used. In Python, a string is a sequence of Unicode character.
Python has a built-in string class named “str” with many handy features. Python Strings are “immutable” which means they cannot be changed after they are created so if we try to change existing string Python interpreter creates new string instead of changing the existing one.
How to create String?
Strings can be created by enclosing characters inside a single [‘] quote or double [“] quotes. Even triple [“””] quotes can be used in Python to enclose string but generally it is used to represent multiline strings and docstrings [multiline comments]
# All the commands are similar In: print('welcome to Reboot') Out: welcome to Reboot In: print("welcome to Reboot") Out: welcome to Reboot #triple quotes string can extend upto multiple lines In: print("""Hello, welcome to Reboot""") Out: Hello, welcome to Reboot
Accessing Individual Characters in a String
We can access individual characters of the String by two ways:
1. Using for loop
using for loop we can display and perform other operations on individual charactes of string sequentially
name = 'Reboot' for ch in name: print(ch) #ch is temporary variable of the for loop Out: R e b o o t
2. Using Indexing
Indexing is the numeric representation of characters in a String. Indexing starts at 0, so the index of the first character is 0, the index of the second character is 1, and so on…
The index must be an integer. We can’t use float or other types, this will result into TypeError.If we try to access a character out of index range it will raise an IndexError .
Python allows negative indexing for its strings. The index of -1 refers to the last item, -2 to the second last item and so on.
now let’s try all the possibilities with the examples:
name = 'Reboot Academy'
print('name = ', name)
print(name[1])#1
print(name[5])#2
print(name[-1])#3
print(name[15])#4 Error: Index out of range
print(name[1.5])#5 Error: Index must be an integer [comment #4 statement to execute #5 statement]
Out:
name = Reboot Academy
e
t
y
IndexError: string index out of range
TypeError: string indices must be integers
String Concatenation ( + )
Combining two or more strings together into a single one is called concatenation.
The + operator is used in Python to concatenate two string literals together.
str1="Reboot"
str2="Academy"
str3= str1 + str2
print(str3)
#OR
print(str1 + str2)
Out:
RebootAcademy
RebootAcademy
Repetition Operator ( * )
The Repetition (*) operator is used to repeat the string for a given number of times.
str1="Hi"
print(str1 * 3) #repetition operator multiply sequence with int type of value only.
Out:
HiHiHi
Change or Delete a string
Strings are immutable, which means that once they are created, they cannot be changed. We can simply reassign different strings to the same name.
#insert image
Change the character of the String
str1="Hello"
str1[0]='y' #Error at this line
print(str1)
Out:
TypeError: 'str' object does not support item assignment
Delete or remove characters from a string. Though we can not delete the character from string, but deleting the string entirely is possible using the keyword del
str1="Hello"
del str1[0] #Error at this line
print(str1)
Out:
TypeError: 'str' object doesn't support item deletion
Deleting entire String
str1="Hello"
del str1
print(str1) #Error at this line
Out:
NameError: name 'str1' is not defined
String Slicing
Slicing expressions are used to select a range of characters from a string.
Syntax: string[start : end]
The expression will return a string containing a copy of the characters from start up to (but not including) end.
name = 'Reboot Academy'
print('name = ', name)
#1
print(name[1:4])
#2
print(name[:5])
#3
print(name[7:])
#4
print(name[:])
#5
print(name[-5:])
#6
print(name[0:len(name)])
#7 with Stepping
letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
print(letters[0:26:2])
Out:
name = Reboot Academy
ebo
Reboo
Academy
Reboot Academy
ademy
Reboot Academy
ACEGIKMOQSUWY
Note: Invalid indexes do not cause slicing expressions to raise an exception.
String Search Operation
operator in and not in are used to determine whether one string is contained in another string or not.
#Using 'in' operator
text = 'seven years ago'
if 'seven' in text:
print('The string "seven" was found.')
else:
print('The string "seven" was not found.')
#Using 'not in' operator
text = 'seven years ago'
if 'Twelve' not in text:
print('The string "Twelve" was not found.')
else:
print('The string "Twelve" was found.')
String Testing Methods
String class in Python have numerous methods to perform various operations on string.
general format of a string method call: stringvar.method(arguments)
stringvar is the name of string variable
method is the name of the method that is being called
arguments is one or more arguments being passed to the method
In this section we will discuss String testing methods:
1. isdigit()
This method returns true if the string contains only numeric digits and is at least one character in length. Returns false otherwise.
strvar="1234"
print(strvar.isdigit())
Out:
True
2. isalnum()
This method returns true if the string contains only alphabetic letters or digits and is at least one character in length. Returns false otherwise.
strvar="abc1234"
print(strvar.isalnum())
Out:
True
3. isalpha()
This method returns true if the string contains only alphabetic letters and is at least one character in length. Returns false otherwise.
strvar="abc"
print(strvar.isalpha())
Out:
True
4. islower()
This method returns true if all of the alphabetic letters in the string are lowercase, and the string contains at least one alphabetic letter. Returns false otherwise.
strvar="ab123"
print(strvar.islower())
Out:
True
5. isspace()
This method returns true if the string contains only whitespace characters and is at least one character in length. (Whitespace characters are spaces, newlines (\n), and tabs (\t). Returns false otherwise.
strvar=" "
print(strvar.isspace())
Out:
True
6. isupper()
Returns true if all of the alphabetic letters in the string are uppercase, and the string contains at least one alphabetic letter.
Returns false otherwise.
strvar="E123"
print(strvar.isupper())
Out:
True
String Modification Methods
Although strings are immutable, meaning they cannot be modified. To modify strings, we can use some of Pythons built-in methods.
1. lower()
This method returns a copy of the string with all alphabetic letters converted to lowercase. Any character that is already lowercase, or is not an alphabetic letter, is unchanged.
strvar = "HELLO world"
print(strvar.lower())
Out:
hello world
2. lstrip()
This method returns a copy of the string with all leading whitespace characters removed. Leading whitespace characters are spaces, newlines (\n), and tabs (\t) that appear at the beginning of the string.
strvar = " Hello World"
print(strvar) #without function
print(strvar.lstrip()) #with function
Out:
Hello World
Hello World
3. lstrip(char)
The char argument is a string containing a character. Returns a copy of the string with all instances of char that appear at the beginning of the string removed.
strvar = "###Hello World"
print(strvar) #without function
print(strvar.lstrip('#')) #with function
Out:
###Hello World
Hello World
4. rstrip()
This method returns a copy of the string with all trailing whitespace characters removed. Trailing whitespace characters are spaces, newlines (\n), and tabs (\t) that appear at the end of the string.
str1 = "Hello "
str2 = "World"
strvar= str1 + str2
print(strvar) #without function
str1 = str1.rstrip()
strvar = str1 + str2
print(strvar) #with function
Out:
Hello World
HelloWorld
5. rstrip(char)
The char argument is a string containing a character. The method returns a copy of the string with all instances of char that appear at the end of the string removed.
str1 = "Hello-"
str2 = "World"
strvar= str1 + str2
print(strvar) #without function
str1 = str1.rstrip('-')
strvar = str1 + str2
print(strvar) #with function
Out:
Hello-World
HelloWorld
6. strip()
This method returns a copy of the string with all leading and trailing whitespace characters removed.
str1 = " Hello "
str2 = "World"
strvar= str1 + str2
print(strvar) #without function
str1 = str1.strip()
strvar = str1 + str2
print(strvar) #with function
Out:
Hello World
HelloWorld
7. strip(char)
This method returns a copy of the string with all instances of char that appear at the beginning and the end of the string removed.
str1 = "###Hello###"
str2 = "World"
strvar= str1 + str2
print(strvar) #without function
str1 = str1.strip('#')
strvar = str1 + str2
print(strvar) #with function
Out:
###Hello###World
HelloWorld
8. upper()
This method returns a copy of the string with all alphabetic letters converted to uppercase. Any character that is already uppercase, or is not an alphabetic letter, is unchanged.
str1 = "hello World 123"
print(str1.upper())
Out:
HELLO WORLD 123
String Searching and Replacing Methods
While doing programming we commonly need to search for substrings, or strings that appear within other strings. Now lets list out some of the Python string methods that search for substrings, as well as a method that replaces the occurrences of a substring with another string.
1. endswith(substring)
The substring argument is a string. The method returns true if the string ends with substring.
str1 = "samplefile.txt"
if str1.endswith(".txt"):
print("its a text file")
else:
print("its not a text file")
Out:
its a text file
2. find(substring)
The substringargument is a string. The method returns the lowest index in the string where substring is found. If substring is not found, the method returns -1.
str1 = "free sample data for testing purpose"
position = str1.find("sample")
if position != -1:
print("found at position" , position)
else:
print("not found")
Out:
found at position 5
3. startswith(substring)
The substring argument is a string. The method returns true if the string starts with substring.
str1 = "final_version1.txt"
if str1.startswith("final"):
#user can write logic for final version files
#print command is just for example
print("its a final version file")
else:
print("its not a final version file")
Out:
Its a final version file
4. replace(old, new)
The old and new arguments are both strings. The method returns a copy of the string with all instances of old replaced by new.
string = 'Four score and eight years ago'
new_string = string.replace('years', 'days')
print(new_string)
Out:
Four score and eight days ago
Splitting a String
Strings in Python have a method named split that returns a list containing the words in the string.
#This program demonstrates the split method.
def main():
# Create a string with multiple words.
my_string = 'alpha beta gama'
# Split the string.
word_list = my_string.split()
# Print the list of words.
print(word_list)
# Call the main function.
main()
Out:
['alpha', 'beta', 'gama']
By default, the split method uses spaces as separators, we can specify a different separator by passing it as an argument to the split method. For example,
date_string = ’11/26/2017′
date_list = date_string.split(‘/’)
# This program calls the split method, using the
# '/' character as a separator.
def main():
# Create a string with a date.
date_string = '11/26/2017'
# Split the date.
date_list = date_string.split('/')
# Display each piece of the date.
print('Month:', date_list[0])
print('Day:', date_list[1])
print('Year:', date_list[2])
# Call the main function.
main()
Out:
Month: 11
Day: 26
Year: 2017
No Comments