0% found this document useful (0 votes)
4 views6 pages

Python String Notes

The document provides an overview of strings in Python, explaining their characteristics, such as immutability, and various operations like concatenation, repetition, membership, and slicing. It also details built-in string methods, including examples for each method, demonstrating their functionality. Overall, it serves as a comprehensive guide for understanding and manipulating strings in Python.

Uploaded by

mohokdas07
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Python String Notes

The document provides an overview of strings in Python, explaining their characteristics, such as immutability, and various operations like concatenation, repetition, membership, and slicing. It also details built-in string methods, including examples for each method, demonstrating their functionality. Overall, it serves as a comprehensive guide for understanding and manipulating strings in Python.

Uploaded by

mohokdas07
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MEDHA COMPUTER STRING IN PYTHON

COACHING CENTER Vidyasagar Path, Naihati, PH: 8013651084


String is a sequence, which is made up of one or more UNICODE characters. Here the
character can be a letter, digit, whitespace or any other symbol. A string can be created by
enclosing one or more characters in single, double or triple quote.
Example
>>> str1 = 'Hello World!'
>>> str2 = "Hello World!"
>>> str3 = """Hello World!"""
>>> str4 = '''Hello World!' '

String is Immutable
A string is an immutable data type. It means that the contents of the string cannot be
changed after it has been created. An attempt to do this would lead to an error.

>>> str1 = "Hello World!"


#if we try to replace character 'e' with 'a'
>>> str1[1] = 'a'
TypeError: 'str' object does not support item Assignment

String Operations
As we know that string is a sequence of characters. Python allows certain operations on
string data type, such as concatenation, repetition, membership and slicing.
Concatenation: To concatenate means to join. Python allows us to join two strings using
concatenation operator plus which is denoted by symbol +.

>>> str1 = 'Hello' #First string


>>> str2 = 'World!' #Second string
>>> str1 + str2 #Concatenated strings
'HelloWorld!' #str1 and str2 remain same

#after this operation.


>>> str1
'Hello'
>>> str2
'World!'

Repetition: Python allows us to repeat the given string using repetition operator, which is
denoted by symbol *.
#assign string 'Hello' to str1
>>> str1 = 'Hello'
#repeat the value of str1 2 times
>>> str1 * 2
'HelloHello'
#repeat the value of str1 5 times
>>> str1 * 5
'HelloHelloHelloHelloHello'
Note: str1 still remains the same after the use of repetition operator.

Membership: Python has two membership operators 'in' and 'not in'.

1
MEDHA COMPUTER STRING IN PYTHON
COACHING CENTER Vidyasagar Path, Naihati, PH: 8013651084
The 'in' operator takes two strings and returns True if the first string appears as a
substring in the second string, otherwise it returns False.
>>> str1 = 'Hello World!'
>>> 'W' in str1
True
>>> 'Wor' in str1
True
>>> 'My' in str1
False
The 'not in' operator also takes two strings and returns True if the first string does
not appear as a substring in the second string, otherwise returns False.
>>> str1 = 'Hello World!'
>>> 'My' not in str1
True
>>> 'Hello' not in str1
False

Slicing: In Python, to access some part of a string or substring, we use a method called
slicing.
>>> str1 = 'Hello World!'
#gives substring starting from index 1 to 4
>>> str1[1:5]
'ello'
#gives substring starting from 7 to 9
>>> str1[7:10]
'orl'
#index that is too big is truncated down to the end of the string
>>> str1[3:20]
'lo World!'
#first index > second index results in an empty '' string
>>> str1[7:2]
If the first index is not mentioned, the slice starts from index gives substring from index 0
to 4
>>> str1[:5]
'Hello'
If the second index is not mentioned, the slicing is done till the length of the string. gives
substring from index 6 to end
>>> str1[6:]
'World!'
The slice operation can also take a third index that specifies the ‘step size’.
>>> str1[0:10:2]
'HloWr'
>>> str1[0:10:3]
'HlWl'
Negative indexes can also be used for slicing.
#characters at index -6,-5,-4,-3 and -2 are sliced
>>> str1[-6:-1]

2
MEDHA COMPUTER STRING IN PYTHON
COACHING CENTER Vidyasagar Path, Naihati, PH: 8013651084
'World'
If we ignore both the indexes and give step size as -1 str1 string is obtained in the reverse
order
>>> str1[::-1]
'!dlroW olleH'

STRING METHODS OR BUILT IN FUNCTION

METHOD DESCRIPTION EXAMPLE

>>> str1 = 'Hello World!'


Returns the length of the given
len() >>> len(str1)
string
12

Returns the string with first letter >>> str1 = 'hello WORLD!'
title() of every word in the string in >>> [Link]()
uppercase and rest in lowercase 'Hello World!'

Returns the string with all >>> str1 = 'hello WORLD!'


lower() uppercase letters converted to >>> [Link]()
lowercase 'hello world!'

Returns the string with all >>> str1 = 'hello WORLD!'


upper() lowercase letters converted >>> [Link]()
to uppercase 'HELLO WORLD!'

Returns number of times


>>> str1 = 'Hello World! Hello Hello'
substring str occurs in the given
>>> [Link]('Hello',12,25)
string. If we do not give start index
count(str, start, end) 2
and end index then searching
>>> [Link]('Hello')
starts from index0 and ends at
3
length of the string

Returns the first occurrence of >>> str1 = 'Hello World! Hello Hello'
index of substring str occurring >>> [Link]('Hello',10,20)
in the given string. If we do not 13
give start and end then searching >>> [Link]('Hello',15,25)
find(str, start, end) starts from index 0 and ends 19
at length of the string. If the >>> [Link]('Hello')
substring is not present in the 0
given string, then the function >>> [Link]('Hee')
returns -1 -1

3
MEDHA COMPUTER STRING IN PYTHON
COACHING CENTER Vidyasagar Path, Naihati, PH: 8013651084
METHOD DESCRIPTION EXAMPLE

>>> str1 = 'Hello World! Hello


Hello'
Same as find() but raises an
>>> [Link]('Hello')
index(str, start, end) exception if the substring is not
0
present in the given string
>>> [Link]('Hee')
ValueError: substring not found

>>> str1 = 'Hello World!'


>>> [Link]('World!')
Returns True if the given string True
endswith() ends with the supplied substring >>> [Link]('!')
otherwise returns False True
>>> [Link]('lde')
False

>>> str1 = 'Hello World!'


Returns True if the given string >>> [Link]('He')
startswith() starts with the supplied substring True
otherwise returns False >>> [Link]('Hee')
False

>>> str1 = 'HelloWorld'


>>> [Link]()
Returns True if characters of the
True
given string are either alphabets
>>> str1 = 'HelloWorld2'
or numeric. If whitespace or
isalnum() >>> [Link]()
special symbols are part of the
True
given string or the string is empty
>>> str1 = 'HelloWorld!!'
it returns False
>>> [Link]()
False

>>> str1 = 'hello world!'


>>> [Link]()
True
>>> str1 = 'hello 1234'
>>> [Link]()
Returns True if the string is non-
True
empty and has all lowercase
>>> str1 = 'hello ??'
alphabets, or has at least one
islower() >>> [Link]()
character as lowercase alphabet
True
and rest are non-alphabet
>>> str1 = '1234'
characters
>>> [Link]()
False
>>> str1 = 'Hello World!'
>>> [Link]()
False

4
MEDHA COMPUTER STRING IN PYTHON
COACHING CENTER Vidyasagar Path, Naihati, PH: 8013651084
METHOD DESCRIPTION EXAMPLE

>>> str1 = 'HELLO WORLD!'


>>> [Link]()
True
>>> str1 = 'HELLO 1234'
>>> [Link]()
Returns True if the string is non-
True
empty and has all uppercase
>>> str1 = 'HELLO ??'
alphabets, or has at least one
isupper() >>> [Link]()
character as uppercase character
True
and rest are non-alphabet
>>> str1 = '1234'
characters
>>> [Link]()
False
>>> str1 = 'Hello World!'
>>> [Link]()
False

>>> str1 = ' \n \t \r'


Returns True if the string is non- >>> [Link]()
empty and all characters are white True
isspace()
spaces (blank, tab, newline, >>> str1 = 'Hello \n'
carriage return) >>> [Link]()
False

>>> str1 = 'Hello World!'


Returns True if the string is non-
>>> [Link]()
empty and title case, i.e., the first
True
istitle() letter of every word in the string in
>>> str1 = 'hello World!'
uppercase and rest
>>> [Link]()
in lowercase
False

Returns the string after removing >>> str1 = ' Hello World!'
lstrip() the spaces only on the left of the >>> [Link]()
string 'Hello World! '

Returns the string after removing >>> str1 = ' Hello World!'
rstrip() the spaces only on the right of the >>> [Link]()
string ' Hello World!'

Returns the string after removing >>> str1 = ' Hello World!'
strip() the spaces both on the left and the >>> [Link]()
right of the string 'Hello World!'

5
MEDHA COMPUTER STRING IN PYTHON
COACHING CENTER Vidyasagar Path, Naihati, PH: 8013651084
METHOD DESCRIPTION EXAMPLE

>>> str1 = 'Hello World!'


>>> [Link]('o','*')
'Hell* W*rld!'
>>> str1 = 'Hello World!'
Replaces all occurrences of old
replace(oldstr, newstr) >>> [Link]('World','Country')
string with the new string
'Hello Country!'
>>> str1 = 'Hello World! Hello'
>>> [Link]('Hello','Bye')
'Bye World! Bye'

>>> str1 = ('HelloWorld!')


Returns a string in which the
>>> str2 = '-' #separator
join() characters in the string have been
>>> [Link](str1)
joined by a separator
'H-e-l-l-o-W-o-r-l-d-!'

Partitions the given string at the


first occurrence of the substring
(separator) and returns the string
partitioned into three parts.
>>> str1 = 'India is a Great Country'
>>> [Link]('is')
1. Substring before the separator
partition() ('India ', 'is', ' a Great Country')
2. Separator
>>> [Link]('are')
3. Substring after the separator
('India is a Great Country','','')
If the separator is not found in the
string, it returns the whole string
itself and two empty strings

>>> str1 = 'India is a Great Country'


Returns a list of words delimited >>> [Link]()
by the specified substring. If no ['India','is','a','Great','Country']
split()
delimiter is given then words are >>> str1 = 'India is a Great Country'
separated by space. >>> [Link]('a')
['Indi', ' is ', ' Gre', 'tCountry']

You might also like