Strings

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Strings

In Python, a string is a sequence of characters. For example, "hello" is a string containing a


sequence of characters 'h', 'e', 'l', 'l', and 'o'. We use single quotes or double quotes to represent a
string in Python. For example,
# create a string using double quotes
string1 = "Python programming"
# create a string using single quotes
string1 = 'Python programming'
Here, we have created a string variable named string1. The variable is initialized with the
string "Python Programming". Here, we have used double quotes to represent strings, but we can
use single quotes too.
Access String Characters in Python
We can access the characters in a string in three ways.
• Indexing: One way is to treat strings as a list and use index values. For example,
greet = 'hello'
# access 1st index element
print(greet[1]) # "e"
• Negative Indexing: Similar to a list, Python allows negative indexing for its strings. For
example,
greet = 'hello'
# access 4th last element
print(greet[-4]) # "e"
• Slicing: Access a range of characters in a string by using the slicing operator colon :. For
example,
greet = 'Hello'
# access character from 1st index to 3rd index
print(greet[1:4]) # "ell"
Note: If we try to access an index out of the range or use numbers other than an integer, we will
get errors.
Python Strings are Immutable
In Python, strings are immutable. That means the characters of a string cannot be changed. For
example,
message = 'Hola Amigos'
message[0] = 'H'
print(message)
Output
TypeError: 'str' object does not support item assignment
However, we can assign the variable name to a new string. For example,
message = 'Hola Amigos'
# assign new string to message variable
message = 'Hello Friends'
print(message); # prints "Hello Friends"
Python Multiline String
We can also create a multiline string in Python. For this, we use triple double quotes """ or triple
single quotes '''. For example,
# multiline string
message = """
Never gonna give you up
Never gonna let you down
"""
print(message)
Output
Never gonna give you up
Never gonna let you down
In the above example, anything inside the enclosing triple quotes is one multiline string.

Python String Operations


Many operations can be performed with strings, which makes it one of the most used data types in
Python.
1. Compare Two Strings
We use the == operator to compare two strings. If two strings are equal, the operator returns True.
Otherwise, it returns False.
For example,
str1 = "Hello, world!"
str2 = "I love Swift."
str3 = "Hello, world!"
# compare str1 and str2
print(str1 == str2)
# compare str1 and str3
print(str1 == str3)
Output
False
True
In the above example,
1. str1 and str2 are not equal. Hence, the result is False.
2. str1 and str3 are equal. Hence, the result is True.

2. Join Two or More Strings


In Python, we can join (concatenate) two or more strings using the + operator.
greet = "Hello, "
name = "Jack"
# using + operator
result = greet + name
print(result)
# Output: Hello, Jack
In the above example, we have used the + operator to join two strings: greet and name.
Iterate Through a Python String
We can iterate through a string using a for loop. For example,
greet = 'Hello'
# iterating through greet string
for letter in greet:
print(letter)
Output
H
e
l
l
o

Python String Length


In Python, we use the len() method to find the length of a string. For example,
greet = 'Hello'
# count length of greet string
print(len(greet))

# Output: 5

String Membership Test


We can test if a substring exists within a string or not, using the keyword in.
print('a' in 'program') # True
print('at' not in 'battle') # False
Methods of Python String

→ upper()
The upper() method converts all lowercase characters in a string into uppercase characters and
returns it.
Example
message = 'python is fun'
# convert message to uppercase
print(message.upper())
# Output: PYTHON IS FUN
The syntax of upper() method is:
string.upper()

• upper() Parameters
upper() method doesn't take any parameters.

• upper() Return Value


upper() method returns the uppercase string from the given string. It converts all lowercase
characters to uppercase.
If no lowercase characters exist, it returns the original string.
Example 1: Convert a string to uppercase
# example string
string = "this should be uppercase!"
print(string.upper())
# string with numbers
# all alphabets should be lowercase
string = "Th!s Sh0uLd B3 uPp3rCas3!"
print(string.upper())
Output
THIS SHOULD BE UPPERCASE!
TH!S SH0ULD B3 UPP3RCAS3!

Example 2: How upper() is used in a program?


# first string
firstString = "python is awesome!"
# second string
secondString = "PyThOn Is AwEsOmE!"
if(firstString.upper() == secondString.upper()):
print("The strings are same.")
else:
print("The strings are not same.")
Output
The strings are same.

→ swapcase()
The swapcase() method returns the string by converting all the characters to their opposite
letter case( uppercase to lowercase and vice versa).
Example
name = "JoHn CeNa"
# converts lowercase to uppercase and vice versa
print(name.swapcase())
# Output: jOhN cEnA

• swapcase() Syntax
The syntax of the swapcase() method is:
string.swapcase()

• swapcase() Parameters
The swapcase() method doesn't take any parameters.

• swapcase() Return Value


The swapcase() method returns:
• the string after converting its uppercase characters to lowercase, and lowercase characters
to uppercase.

Example 1: Python swapcase()


sentence1 = "THIS SHOULD ALL BE LOWERCASE."
# converts uppercase to lowercase
print(sentence1.swapcase())
sentence2 = "this should all be uppercase."
# converts lowercase to uppercase
print(sentence2.swapcase())
sentence3 = "ThIs ShOuLd Be MiXeD cAsEd."
# converts lowercase to uppercase and vice versa
print(sentence3.swapcase())
Output
this should all be lowercase.
THIS SHOULD ALL BE UPPERCASE.
tHiS sHoUlD bE mIxEd CaSeD.
In the above example, we have used the swapcase() method to convert lowercase characters
to uppercase and vice versa. Here,
• sentence1.swapcase() - converts every uppercase character in "THIS SHOULD ALL BE
LOWERCASE." to lowercase
• sentence2.swapcase() - converts every lowercase character in "this should all be
uppercase." to uppercase
• sentence3.swapcase() - converts the mixed string sentence3 i.e. "ThIs ShOuLd Be MiXeD
cAsEd." to opposite letter case

→ lower()

The lower() method converts all uppercase characters in a string into lowercase
characters and returns it.
Example
message = 'PYTHON IS FUN'
# convert message to lowercase
print(message.lower())
# Output: python is fun

• Syntax of String lower()


The syntax of lower() method is:
string.lower()

• lower() Parameters()
lower() method doesn't take any parameters.

• lower() Return value


lower() method returns the lowercase string from the given string. It converts all uppercase
characters to lowercase.
If no uppercase characters exist, it returns the original string.
Example 1: Convert a string to lowercase
# example string
string = "THIS SHOULD BE LOWERCASE!"
print(string.lower())
# string with numbers
# all alphabets should be lowercase
string = "Th!s Sh0uLd B3 L0w3rCas3!"
print(string.lower())
Output
this should be lowercase!
th!s sh0uld b3 l0w3rcas3!

→ partition()
The syntax of partition() is:
string.partition(separator)

• partition() Parameters()
The partition() method takes a string parameter separator that separates the string at the first
occurrence of it.

• Return Value from partition()


The partition method returns a 3-tuple containing:
• the part before the separator, separator parameter, and the part after the separator if the
separator parameter is found in the string
• the string itself and two empty strings if the separator parameter is not found
Example: How partition() works?
string = "Python is fun"
# 'is' separator is found
print(string.partition('is '))
# 'not' separator is not found
print(string.partition('not '))
string = "Python is fun, isn't it"
# splits at first occurence of 'is'
print(string.partition('is'))
Output
('Python ', 'is ', 'fun')
('Python is fun', '', '')
('Python ', 'is', " fun, isn't it")

→ rpartition()
The syntax of rpartition() is:
string.rpartition(separator)

• rpartition() Parameters()
rpartition() method takes a string parameter separator that separates the string at the last
occurrence of it.

• Return Value from rpartition()


rpartition() method returns a 3-tuple containing:
• the part before the separator, separator parameter, and the part after the separator if the
separator parameter is found in the string
• two empty strings, followed by the string itself if the separator parameter is not found
Example: How rpartition() works?
string = "Python is fun"
# 'is' separator is found
print(string.rpartition('is '))
# 'not' separator is not found
print(string.rpartition('not '))
string = "Python is fun, isn't it"
# splits at last occurence of 'is'
print(string.rpartition('is'))
Output
('Python ', 'is ', 'fun')
('', '', 'Python is fun')
('Python is fun, ', 'is', "n't it")
→ replace()
The replace() method replaces each matching occurrence of a substring with another string.
Example
text = 'bat ball'
# replace 'ba' with 'ro'
replaced_text = text.replace('ba', 'ro')
print(replaced_text)
# Output: rot roll

• replace() Syntax
Its syntax is:
str.replace(old, new [, count])

• replace() Arguments
The replace() method can take a maximum of three arguments:
• old - the old substring we want to replace
• new - new substring which will replace the old substring
• count (optional) - the number of times you want to replace the old substring with
the new string
Note: If count is not specified, the replace() method replaces all occurrences of the old substring
with the new string.

• replace() Return Value


The replace() method returns a copy of the string where the old substring is replaced with
the new string. The original string remains unchanged.
If the old substring is not found, it returns a copy of the original string.
Example 1: Using replace()
song = 'cold, cold heart'
# replacing 'cold' with 'hurt'
print(song.replace('cold', 'hurt'))
song = 'Let it be, let it be, let it be, let it be'
# replacing only two occurrences of 'let'
print(song.replace('let', "don't let", 2))
Output
hurt, hurt heart
Let it be, don't let it be, don't let it be, let it be
Example:-
song = 'cold, cold heart'
replaced_song = song.replace('o', 'e')
# The original string is unchanged
print('Original string:', song)
print('Replaced string:', replaced_song)
song = 'let it be, let it be, let it be'
# maximum of 0 substring is replaced
# returns copy of the original string
print(song.replace('let', 'so', 0))

→ rfind()
The syntax of rfind() is:
str.rfind(sub[, start[, end]] )

• rfind() Parameters
rfind() method takes a maximum of three parameters:
✓ sub - It's the substring to be searched in the str string.
✓ start and end (optional) - substring is searched within str[start:end]

• Return Value from rfind()


rfind() method returns an integer value.
• If substring exists inside the string, it returns the highest index where substring is found.
• If substring doesn't exist inside the string, it returns -1.
Example 1: rfind() With No start and end Argument
quote = 'Let it be, let it be, let it be'
result = quote.rfind('let it')
print("Substring 'let it':", result)
result = quote.rfind('small')
print("Substring 'small ':", result)
result = quote.rfind('be,')
if (result != -1):
print("Highest index where 'be,' occurs:", result)
else:
print("Doesn't contain substring")
Output
Substring 'let it': 22
Substring 'small ': -1
Highest index where 'be,' occurs: 18
Example 2: rfind() With start and end Arguments
quote = 'Do small things with great love'
# Substring is searched in 'hings with great love'
print(quote.rfind('things', 10))
# Substring is searched in ' small things with great love'
print(quote.rfind('t', 2))
# Substring is searched in 'hings with great lov'
print(quote.rfind('o small ', 10, -1))
# Substring is searched in 'll things with'
print(quote.rfind('th', 6, 20))
Output
-1
25
-1
18

→ count()
The count() method returns the number of occurrences of a substring in the given string.
Example
message = 'python is popular programming language'
# number of occurrence of 'p'
print('Number of occurrence of p:', message.count('p'))
# Output: Number of occurrence of p: 4

• Syntax of String count


The syntax of count() method is:
string.count(substring, start=..., end=...)

• count() Parameters
count() method only requires a single parameter for execution. However, it also has two optional
parameters:
• substring - string whose count is to be found.
• start (Optional) - starting index within the string where search starts.
• end (Optional) - ending index within the string where search ends.
Note: Index in Python starts from 0, not 1.

• count() Return Value


count() method returns the number of occurrences of the substring in the given string.
Example 1: Count number of occurrences of a given substring
# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count(substring)
# print count
print("The count is:", count
Output
The count is: 2

Example 2: Count number of occurrences of a given substring using start and end
# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count(substring, 8, 25)
# print count
print("The count is:", count)
Output
The count is: 1
Here, the counting starts after the first i has been encountered, i.e. 7th index position. And, it ends
before the last i, i.e. 25th index position.

→ rstrip()

The rstrip() method returns a copy of the string with trailing characters removed
(based on the string argument passed).
Example

title = 'Python Programming '

# remove trailing whitespace from title


result = title.rstrip()

print(result)

# Output: Python Programming

• Syntax of String rstrip()

The syntax of rstrip() is:

string.rstrip([chars])

• rstrip() Parameters

• chars (optional) - a string specifying the set of trailing characters to be removed.

The rstrip() removes characters from the right based on the argument (a string specifying
the set of characters to be removed).

If chars argument is not provided, all whitespaces on the right are removed from the string.

• Return Value from rstrip()

The rstrip() returns a copy of the string with trailing characters stripped.

All combinations of characters in chars argument are removed from the right of the string
until the first mismatch.

random_string = 'this is good '

# Trailing whitespace are removed

print(random_string.rstrip())

# 'si oo' are not trailing characters so nothing is removed

print(random_string.rstrip('si oo'))

# in 'sid oo', 'd oo' are the trailing characters, 'ood' is removed from the string

print(random_string.rstrip('sid oo')) website = 'www.programiz.com/'


print(website.rstrip('m/.'))

Output

this is good

this is good

this is g

www.programiz.co

→ split()

The split() method breaks down a string into a list of substrings using a chosen separator.

Example

text = 'Python is fun'

# split the text from space

print(text.split())

# Output: ['Python', 'is', 'fun']

• split() Syntax

str.split(separator, maxsplit)

• split() Parameters

The split() method takes a maximum of 2 parameters:

• separator (optional) - Specifies the delimiter used to split the string. If not provided,
whitespace is used as the default delimiter.
• maxsplit (optional) - Determines the maximum number of splits. If not provided, the
default value is -1, which means there is no limit on the number of splits.

• split() Return Value


The split() method returns a list of strings.
text= 'Split this string'

# splits using space

print(text.split())

grocery = 'Milk, Chicken, Bread'

# splits using ,

print(grocery.split(', '))

# splits using :

# doesn't split as grocery doesn't have :

print(grocery.split(':'))

Output

['Split', 'this', 'string']

['Milk', 'Chicken', 'Bread']

['Milk, Chicken, Bread']

Here,

• text.split() - splits the string into a list of substrings at each space character.
• grocery.split(', ') - splits the string into a list of substrings at each comma and space
character.
• grocery.split(':') - since there are no colons in the string, split() does not split the
string.

Example: split() with maxsplit


We can use the maxsplit parameter to limit the number of splits that can be
performed on a string.
grocery = 'Milk#Chicken#Bread#Butter'

# maxsplit: 1

print(grocery.split('#', 1))

# maxsplit: 2

print(grocery.split('#', 2))

# maxsplit: 5

print(grocery.split('#', 5))

# maxsplit: 0

print(grocery.split('#', 0))

Output

['Milk', 'Chicken#Bread#Butter']

['Milk', 'Chicken', 'Bread#Butter']

['Milk', 'Chicken', 'Bread', 'Butter']

['Milk#Chicken#Bread#Butter']

Note: If maxsplit is specified, the list will have a maximum of maxsplit+1 items.

→ splitlines()

The splitlines() method splits the string at line breaks and returns a list.

Example

# \n is a line boundary

sentence = 'I\nlove\nPython\nProgramming.'

# returns a list after spliting string at line breaks

resulting_list = sentence.splitlines()
print(resulting_list)

# Output: ['I', 'love', 'Python', 'Programming.']

• splitlines() Syntax

string.splitlines([keepends])

Here, keepends can be True or any number.

• splitlines() Parameters

The splitlines() method can take a single parameter:

keepends(optional) - it determines whether line breaks are included in the resulting list or
not. It's value can be True or any number.

• splitlines() Return Value

The splitlines() method returns:

• a list of lines in the string.

If there are not line break characters, it returns a list with a single item (a single line).

Example 1: Python String splitlines()

# '\n' is a line break

grocery = 'Milk\nChicken\nBread\rButter'

# returns a list after splitting the grocery string

print(grocery.splitlines())

Output

['Milk', 'Chicken', 'Bread', 'Butter']

In the above example, we have used the splitlines() method to split the grocery string
i.e. 'Milk\nChicken\r\nBread\rButter' at the line breaks.
Here, grocery.splitlines() splits grocery at line break '\n' and returns a list '['Milk', 'Chicken',
'Bread', 'Butter']' after removing the line break.

→ startswith()

The startswith() method returns True if a string starts with the specified prefix(string). If not,
it returns False.

Example

message = 'Python is fun'

# check if the message starts with Python

print(message.startswith('Python'))

# Output: True

• Syntax of String startswith()

The syntax of startswith() is:

str.startswith(prefix[, start[, end]])

• startswith() Parameters

startswith() method takes a maximum of three parameters:

• prefix - String or tuple of strings to be checked


• start (optional) - Beginning position where prefix is to be checked within the string.
• end (optional) - Ending position where prefix is to be checked within the string.

• startswith() Return Value

startswith() method returns a boolean.

• It returns True if the string starts with the specified prefix.


• It returns False if the string doesn't start with the specified prefix.
Example 1: startswith() Without start and end Parameters

text = "Python is easy to learn."

result = text.startswith('is easy')

# returns False

print(result)

result = text.startswith('Python is ')

# returns True

print(result)

result = text.startswith('Python is easy to learn.')

# returns True

print(result)

Output

False

True

True

Example 2: startswith() With start and end Parameters

text = "Python programming is easy."

# start parameter: 7

# 'programming is easy.' string is searched

result = text.startswith('programming is', 7)

print(result)
# start: 7, end: 18

# 'programming' string is searched

result = text.startswith('programming is', 7, 18)

print(result)

result = text.startswith('program', 7, 18)

print(result)

Output

True

False

True

→ endswith()

The endswith() method returns True if a string ends with the specified suffix. If not, it
returns False.

Example

message = 'Python is fun'

# check if the message ends with fun

print(message.endswith('fun'))

# Output: True

• Syntax of String endswith()

The syntax of endswith() is:

str.endswith(suffix[, start[, end]])

• endswith() Parameters

The endswith() takes three parameters:


• suffix - String or tuple of suffixes to be checked
• start (optional) - Beginning position where suffix is to be checked within the string.
• end (optional) - Ending position where suffix is to be checked within the string.

• Return Value from endswith()

The endswith() method returns a boolean.

• It returns True if a string ends with the specified suffix.


• It returns False if a string doesn't end with the specified suffix.

Example 1: endswith() Without start and end Parameters

text = "Python is easy to learn."

result = text.endswith('to learn')

# returns False

print(result)

result = text.endswith('to learn.')

# returns True

print(result)

result = text.endswith('Python is easy to learn.')

# returns True

print(result)

Output

False

True

True
Example 2: endswith() With start and end Parameters

text = "Python programming is easy to learn."

# start parameter: 7

# "programming is easy to learn." string is searched

result = text.endswith('learn.', 7)

print(result)

# Both start and end is provided

# start: 7, end: 26

# "programming is easy" string is searched

result = text.endswith('is', 7, 26)

# Returns False

print(result)

result = text.endswith('easy', 7, 26)

# returns True

print(result)

Output

True

False

True

→ isnumeric()

The isnumeric() method checks if all the characters in the string are numeric.
Example

pin = "523"

# checks if every character of pin is numeric

print(pin.isnumeric())

# Output: True

• isnumeric() Syntax

The syntax of the isnumeric() method is:

string.isnumeric()

Here, isnumeric() checks if all characters in string are numeric or not.

• isnumeric() Parameters

The isnumeric() method doesn't take any parameters.

• isnumeric() Return Value

The isnumeric() method returns:

• True -if all characters in the string are numeric


• False -if at least one character is not a numeric

Example 1: Python isnumeric()


symbol_number = "012345"

# returns True as symbol_number has all numeric characters

print(symbol_number.isnumeric())

text = "Python3"

# returns False as every character of text is not numeric

print(text.isnumeric())
Output

True

False

In the above example, we have used the isnumeric() method to check whether every
character in symbol_number and text is numeric or not.

The method returns:

• True - for symbol_number since every character in "012345" are numeric


• False - for text since every character in "Python3" are not numeric

Example 2: isnumeric() with Other Numeric Types


Python treats mathematical characters like numbers, subscripts, superscripts, and
characters having Unicode numeric value properties (like a fraction, roman numerals,
currency numerators) as numeric characters.

The isnumeric() method returns True with these characters. For example

# string with superscript

superscript_string = '²3455'

print(superscript_string.isnumeric())

# string with fraction value

fraction_string = '½123'

print(fraction_string.isnumeric())

Output

True

True

Here, we have used the isnumeric() method with strings that contain superscript and
fraction.
• superscript_string.isnumeric() -returns True because '²3455' contains all numeric
characters.
• fraction_string.isnumeric() - returns True because '½123' contains all numeric
characters.

Python String isdigit()


Python String isalnum()
Python String isdecimal()

→ index()

The index() method returns the index of a substring inside the string (if found). If the
substring is not found, it raises an exception.

Example

text = 'Python is fun'

# find the index of is

result = text.index('is')

print(result)

# Output: 7

• index() Syntax

It's syntax is:

str.index(sub[, start[, end]] )

• index() Parameters

The index() method takes three parameters:

• sub - substring to be searched in the string str.


• start and end(optional) - substring is searched within str[start:end]
• index() Return Value

• If substring exists inside the string, it returns the lowest index in the string where
substring is found.
• If substring doesn't exist inside the string, it raises a ValueError exception.

➢ The index() method is similar to the find() method for strings.


➢ The only difference is that find() method returns -1 if the substring is not found,
whereas index() throws an exception.

Example 1: index() With Substring argument Only

sentence = 'Python programming is fun.'

result = sentence.index('is fun')

print("Substring 'is fun':", result)

result = sentence.index('Java')

print("Substring 'Java':", result)

Output

Substring 'is fun': 19

Traceback (most recent call last):

File "<string>", line 6, in

result = sentence.index('Java')

ValueError: substring not found

Note: Index in Python starts from 0 and not 1. So the occurrence is 19 and not 20.

Example 2: index() With start and end Arguments

sentence = 'Python programming is fun.'

# Substring is searched in 'gramming is fun.'

print(sentence.index('ing', 10))
# Substring is searched in 'gramming is '

print(sentence.index('g is', 10, -4))

# Substring is searched in 'programming'

print(sentence.index('fun', 7, 18))

Output

15

17

Traceback (most recent call last):

File "<string>", line 10, in

print(quote.index('fun', 7, 18))

ValueError: substring not found

Escape Sequences in Python

The escape sequence is used to escape some of the characters present inside a string.

Suppose we need to include both a double quote and a single quote inside a string,

example = "He said, "What's there?""

print(example) # throws error

Since strings are represented by single or double quotes, the compiler will treat "He said,
" as a string. Hence, the above code will cause an error.

To solve this issue, we use the escape character \ in Python.

# escape double quotes

example = "He said, \"What's there?\""


# escape single quotes

example = 'He said, "What\'s there?"'

print(example)

# Output: He said, "What's there?"

Here is a list of all the escape sequences supported by Python.

Escape Sequence Description

\\ Backslash

\' Single quote

\" Double quote

\a ASCII Bell

\b ASCII Backspace

\f ASCII Formfeed

\n ASCII Linefeed

\r ASCII Carriage Return

\t ASCII Horizontal Tab

\v ASCII Vertical Tab

\ooo Character with octal value ooo

\xHH Character with hexadecimal value HH

You might also like