STRINGS



Skip to a subsection :

  1. DIVE INTO THE STRINGS
  2. WORKING WITH STRINGS
  3. METHODS
  4. F-STRINGS



1. DIVE INTO THE STRINGS

A string is a sequence of characters that can be stored and manipulated as a single entity.

Ordered

A string is an ordered sequence, meaning that each character has a fixed position. You can access individual characters in a string using an index

Immutable

Strings are immutable. You cannot modify the characters in a string once it has been created.

Duplicates

Strings allow duplicates. Each character can occur multiple times in a string.

Syntax

Strings are defined by enclosing a sequence of characters in single, double, or triple quotation marks ' ' or " " or """ """.

Utilizing strings

Strings are used to represent text in Python, to communicate with users. Many applications use strings to display messages, prompts, and other types of information to users. Strings are used to store and transmit data over the internet. Many web applications and APIs use strings to represent and transmit data in various formats, such as JSON or XML.

2. WORKING WITH STRINGS

Single quotation marks (' ') and double quotation marks (" ") can be used interchangeably to create strings. It's a matter of personal preference.

s1="Hello World" # double quotation mark
s2='Hello World' # single quotation mark
print(s1) # prints Hello World
print(s2) # prints Hello World
s1==s2 # prints True 

Triple quotation marks (''' ''' or """ """) are used to create multi-line strings. They also allow you to include quotes within the string without the need to escape them, which can make the code more readable.

# a mutli-line string that includes double quotation marks within it :
s="""Single quotation marks (' ') and double quotation marks (" ") can be used interchangeably to create strings. Triple quotation marks are used to create multi-line strings."""

Common operators :

str1='a'
str2='2'
print(str1+str2) # prints 'a2'
print(str1*3) # prints 'aaa'
print('a' in str1) # prints True
print('b' in str1) # prints False
print('a' not in str1) # prints False
print(s[0]) # prints 'a'


3. METHODS

A method is a function that is associated with a specific object or class and performs a specific task. There are many different ways that text data can be manipulated and Python provides a wide range of functionalities for working with strings. You can feel confident that there is likely a method available to help with almost any string-related task.

The methods for working with strings can be categorized into the following groups:

These categories are not official and there are other ways to categorize methods. The goal of these categories is to provide a helpful overview and a way to better understand and organize the tools available


ADDING METHODS

center(): centers the string within a specified width, using a fill character

s="The Black Pearl"
print(s.center(30,'*')) #prints *******The Black Pearl********   

ljust(): left-aligns the string within a specified width, using a fill character

s="The Black Pearl"
print(s.ljust(30,'*')) #prints The Black Pearl***************   

rjust():right-aligns the string within a specified width, using a fill character

s="The Black Pearl"
print(s.rjust(30,'*')) #prints ***************The Black Pearl

zfill(): pads the string with zeros on the left, until it reaches a specified width

JamesBond='7'
print(JamesBond.zfill(3)) # prints 007

CASE MODIFICATION METHODS

lower(): converts the entire string to lowercase

s1="HELLO!"
print(s1.lower()) # prints hello!

upper(): converts the entire string to uppercase

s="Hey there!"
print(s.upper()) # prints HEY THERE!

capitalize(): capitalizes the first character of the string

s="hey there!"
print(s.capitalize()) # prints Hey there!   

title(): capitalizes the first character of each word in the string

s="jack sparrow"
print(s.title()) # prints Jack Sparrow   

swapcase(): swaps the case of all the characters in the string

s="cAPS lOCK WAS ON"
print(s.swapcase()) # prints Caps Lock was on  

casefold():returns a version of the string suitable for caseless comparisons

s="HELLO WORLD"
print(s.casefold()) # prints hello world    

STRIPPING METHODS

strip() : returns a copy of the string with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead.

s1='     extra spaces in a string   '
print(len(s1)) # prints 32
print(s1.strip()) # prints extra spaces in a string
len(s1.strip()) # prints 24   

rstrip(): removes trailing whitespace from the string

s='Readability counts   '
print(len(s)) # prints 21
print(s.rstrip()) # prints Readability counts
print(len(s.rstrip())) # prints 18

SPLITTING METHODS

rpartition() : splits the string into a tuple around a specific substring, starting from the end of the string

tweet="This is another simple string ! #simple"
print(tweet.rpartition('#')) # prints ('This is another simple string ! ', '#', 'simple')   

split(): splits the string into a list of words

s='just ones and zeros.'
print(s.split()) # prints ['just', 'ones', 'and', 'zeros.']     

rsplit : Return a list of the words in the string, using sep as the delimiter string.

s='Never enough time'
print(s.rsplit()) # prints ['Never', 'enough', 'time']   

partition() : splits the string into a tuple around a specific substring

mystring="To be or not to be,"
print(mystring.partition('or')) # prints ('To be ', 'or', ' not to be,')   

join(): Concatenates any number of strings of a interable.

print('-'.join(('c','a','t'))) # prints c-a-t
print('-'.join(['c','a','t'])) # prints c-a-t
print(''.join(('c','a','t'))) # prints cat
print(' '.join(('s','p','a','c','e','s'))) # prints s p a c e s
print(' #'.join(('make a difference','together'))) # prints make a difference #together
print(''.join(('py','thon'))) # prints python
print('-'.join(('1','2','3'))) # prints 1-2-3
print('-'.join((1,2,3))) # prints TypeError: sequence item 0: expected str instance, int found   
concatenate a list of strings

splitlines(): returns a list of the lines in the string, breaking at line boundaries

s='Flat is better than nested. \n Sparse is better than dense.'
print(s.splitlines()) # prints ['Flat is better than nested. ', ' Sparse is better than dense.']  

SEARCH METHODS

endswith(): returns True if string ends with the specified suffix, False otherwise. With optional start, test string beginning at that position. With optional end, stop comparing S at that position. Suffix can also be a tuple of strings to try.

s='servers are slow'
print(s.endswith('low')) # prints True
print(s.endswith('low',13)) # prints True
print(s.endswith('low',14)) # prints False
print(s.endswith('ers',0,7)) # prints True   

find(): returns the lowest index ( first occurence ) in string where substring sub is found, such that sub is contained within [start:end]. Optional arguments start and end are interpreted as in slice notation. -1 if the substring is not found.

s='To be or not to be'
print(s.find('be')) # prints 3
print(s.find('be',4)) # prints 16
print(s.find('o')) # prints  1
print(s.find('o',3,10)) # prints 6
print(s.find('x')) # prints -1    

index(): raises a ValueError exception if the substring is not found. In general, you should use find when you want to handle the possibility of the substring not being found, and index when you want the program to raise an exception if the substring is not found. find() and index() methods are both used to search for substrings within a string.

s='Random numbers are too important to be left to chance'
print(s.index('d')) # prints 3
print(s.index('x')) # prints ValueError: substring not found   

isalnum(): returns True if the string is an alpha-numeric string, False otherwise. Spaces and #are not alphanumeric

s1='#sleepycat' 
print(s1.isalnum()) # prints False
s2='sleepy cat'
print(s2.isalnum()) # prints False
s3='sleepycat'
print(s3.isalnum()) # prints True   

isalpha(): returns True if the string is an alphabetic string, False otherwise. An alphabetic string is a string that consists only of letters (upper or lower case) and is not empty.

s1='This is not an alphabetic string' # spaces are not alphabetic 
s2='Thisisanalphabeticstring'
print(s1.isalpha()) # prints False
print(s2.isalpha()) # prints True   

isascii() : returns True if all characters in the string are ASCII, False otherwise.

string = "Hello, world!"
print(string.isascii()) # prints True
string = "Héllo, world!" #é is a non-ASCII character is not an ASCII character
print(string.isascii())  # prints False   

isdecimal(): returns True if the string is a decimal string, False otherwise. returns False if the string contains any characters that are not decimal digits. The decimal digits are 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9

number1='31416' #contains only decimal digits
print(number1.isdecimal()) # prints True
number2='3.1416' # '.' is not a decimal digit
print(number2.isdecimal()) #prints False  

isdigit() : returns True if the string is a digit string, False otherwise. The isdecimal() and isdigit() methods are both methods that can be used to check if a string contains only digits. the differences between the isdigit and isdecimal methods are not commonly encountered. The cubed symbol (³) can illustrate the difference between the isdecimal and isdigit methods by showing that one method recognizes it as a decimal, while the other does not. The cubed character (³) is represented in Unicode by the character code U+00B3

number1='31416' #contains only decimal digits
print(number1.isdigit()) # prints True
number2='3.1416' # '.' is not a decimal digit
print(number2.isdigit()) # prints False
number3= '\u00B3' # the cubed character (³) unicode 
print(number3.isdigit()) # prints True
print(number3.isdecimal()) # prints False   

isidentifier(): Returns True if the string is a valid Python identifier, False otherwise

s1='helloworld'
s2='hello world' #space can't be used in identifiers
s3="2day" #identifiers can't begin with a digit
print(s1.isidentifier()) # prints True
print(s2.isidentifier()) # prints False
print(s3.isidentifier()) # prints False    

islower(): returns True if the string is a lowercase string

s1='it was a simple comma issue'
s2='It was a simple comma issue'
print(s1.islower()) # prints True
print(s2.islower()) # prints False  

isnumeric(): returns True if the string is a numeric string, False otherwise. The isnumeric() and isdigit() string methods are both used to check if a string contains numeric characters. isnumeric() is a broader method that returns True not only for strings that contain only digits, but also for strings that contain numeric characters from other writing systems, such as Chinese numerals or fractions.

s1='31416'
s2='²'
s3='½'
print(s2.isnumeric()) # prints True
print(s2.isnumeric()) # prints True
print(s2.isdigit()) # prints True
print(s3.isnumeric()) # prints True
print(s3.isdigit()) # prints False   

isprintable()returns True if the string is printable, False otherwise.

s1 = "Why are there so many different string methods in Python?"
s2 = "Why are there so many different string methods in Python?\n" #the newline caracter \n is not a printable caracter
print(s1.isprintable())  # prints True
print(s2.isprintable())  # prints False   

isspace(): returns True if the string is a whitespace string, False otherwise

s1='hello world'
s2=' '
print(s1.isspace()) # prints False
print(s2.isspace())  # prints True

istitle() : returns True if the string is a title-cased string, False otherwise.

book1='The Lord Of The Rings'
book2='The Lord of The Rings' # 'of' is in lower case in book2
print(book1.istitle()) # prints True
print(book2.istitle()) # prints False

isupper() : returns True if the string is an uppercase string, False otherwise.

s1='this is shouting'
s2='This Is Shouting'
s3='THIS IS SHOUTING'
s4='012345'
print(s1.isupper()) # prints False
print(s2.isupper()) # prints False
print(s3.isupper()) # prints True
print(s4.isupper()) # prints False

rfind(): returns the highest index in S where substring sub is found

s='complex or complicated'
print(s.rfind('com')) # prints 11
print(s.rfind('c')) # prints 17
print(s.rfind('c',0,10)) # prints 0
print(s.rfind('w')) # prints -1
print(s.rfind('C')) # prints -1

rindex(): Return the highest index in S where substring sub is found, such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation.

s='complex or complicated'
print(s.rindex('com')) # prints 11
print(s.rindex('c')) # prints 17
print(s.rindex('c',0,7)) # prints 0
print(s.rindex('w')) # prints ValueError: substring not found
print(s.rindex('C')) # prints ValueError: substring not found

startswith: returns True if S starts with the specified prefix, False otherwise. With optional start, test S beginning at that position. With optional end, stop comparing S at that position. prefix can also be a tuple of strings to try.

s='Now is better than never.'
print(s.startswith('N')) # prints True
print(s.startswith('Now')) # prints True
print(s.startswith('n')) # prints False
print(s.startswith('w',2)) # prints True
print(s.startswith('N',2,5)) # prints False  

FORMATTING METHODS

format(): returns a formatted version of string, using substitutions from args and kwargs. The substitutions are identified by braces ('{' and '}').

s = "an average of {days} days a year of sunshine"
print(s.format(days = 230)) # returns an average of 230 days a year of sunshine

format_map(): returns a formatted version of string, using substitutions from args and kwargs. The substitutions are identified by braces ('{' and '}'). It is similar to str.format(mapping) except that str.format(mapping) creates a new dictionary whereas str.format_map(mapping) doesn't.

s='{x}% of your sales come from only {y}% of your customers.'
dictmap = {'x':80,'y':15}
print(s.format_map(dictmap)) # prints 80% of your sales come from only 15% of your customers.

OTHER METHODS

count(): counts the number of occurrences of a specific word in the string

tweet="to be or not to be #overthinking"
print(tweet.count('be')) # prints 2
print(tweet.count('#')) # prints 1
print(tweet.count('question')) # prints 0

replace(): replaces a specific word in the string

s="I am Gandalf, and I will not let evil triumph."
print(s.replace('Gandalf','Dumbledore')) # prints I am Dumbledore, and I will not let evil triumph.

maketrans(): takes two strings as arguments and returns a translation table that can be used with the translate() method. The first string specifies the characters to be replaced, and the second string specifies the replacement characters/B>

original_string = "the greatest adventure of all is yet to come"
translatortable = str.maketrans('ta', 'xo') # 'x' replaces 't' and 'o' replaces 'a' 
print(original_string.translate(translatortable)) # prints xhe greoxesx odvenxure of oll is yex xo come

translate(): eeplaces each character in the string using the given translation table.takes a translation table as its argument, which is created using the str.maketrans() method

original_string2="Debugging is like being a detective in a crime movie where you are also the murderer."
translatortable = str.maketrans('gm', 'lb') # 'l' replaces 'g' and 'b' replaces 'm' 
print(original_string2.translate(translatortable)) #prints Debullinl is like beinl a detective in a cribe bovie where you are also the burderer.

encode(): encodes the string using the codec registered for encoding. You can specify a different encoding as an argument to the encode method, such as utf-16, ascii, or latin-1. The encode method is often used when working with text data that needs to be stored or transmitted in a specific encoding, or when interacting with systems that expect data to be in a specific encoding.

# the prefix b represents bytes objects
s='from one format into another'
print(s.encode()) # prints b'from one format into another'
string = "Hello, World!"  
encoded_string = string.encode("utf-8")
print(encoded_string) # prints b'Hello, World!'

len()built-in function that returns the length of an object. It is not a method of strings specifically, but it can be used to find the length of a strings

s = "This is a simple string"
len(s) # prints 23

4. F-STRINGS

With the introduction of f-strings in Python 3.6, the use of % and str.format() is becoming less common. They simplify the syntax and make the code more readable.

# example 1
version = 3.6
message = f"The current version of Python is {version}."
print(message) # prints The current version of Python is 3.6.

# example 2
x=4
y=5
print(f'the sum of {x} and {y} is {x+y}')
# prints : the sum of 4 and 5 is 9

# example 3
import datetime
today = datetime.date.today()
formatted_date = f"Today's date is {today:%Y-%m-%d}"
print(formatted_date)
# prints : Today's date is 2023-09-25

# example 4
animal_type = "cat"
animal_name = "Minino"
message = (
f"My {animal_type} is named {animal_name}."
f"{animal_name} is a good {animal_type}."
)
# prints :
# My cat is named Minino. Minino is a good cat.

# example 5
letters1='abc'
letters2='efg'
s2 = f'{letters1}\\\\\n{letters2}'
print(s2)
#prints : 
abc\\
efg

Number formatting with f-strings :

number = 314.1592
print(f'{number}') # prints 314.1592
print(f'{number + 1}') # prints 315.1592
print(f'{number // 2}') # prints 157.0
print(f'{number:.2f}') # prints 314.16
print(f'{number:.6f}') # prints 314.159200
print(f'{number:010}') # prints 00314.1592
print(f'{number:%}') # prints 31415.920000%
print(f'{number:e}') # prints 3.141592e+02

Strings methods with f-strings :

my_string = 'chapter one'
print(f'{my_string.title()}') # prints Chapter One
print(f'{my_string.replace("one","two")}') # prints chapter two
print(f'{my_string.title().center(18,"*")}') # prints ***Chapter One****
print(f'{my_string.zfill(15)}') # prints 0000chapter one