Class 12 : Review Of Python Basics

Class 12 | Revision Tour : Strings

By: Archana Shukla and Rajesh Shukla

Strings

String is a sequence of characters surrounded by either single quotation marks, or double quotation marks.
‘hello’ is the same as “hello”.

Assign String to a Variable

Assigning a string to a variable is done with the variable name followed by an equal sign and the string:

Example

>>>a = “Hello”
>>>print(a)

Multiline Strings

You can assign a multiline string to a variable by using three quotes: we can use three double quotes or three single quotes

Example

>>>a = “””A string can hold
any type of known characters
of any scripted language .”””
>>>print(a)

Example

>>>a = ‘’’A string can hold
any type of known characters
of any scripted language .’’’
>>>print(a)

String operations

Concatenation

Joins two strings. ‘+’ operator is used for joining two strings.

Example

>>> str1=’hello’
>>> str2=’world’
>>> print(str1+str2)
helloworld

Repetition

Creates multiple copies of same string. ‘*’ operator is used.

Example

>>> str1=’hello’
>>> print(str1*3)
hellohellohello

Membership

in and not in. Returns true if the given character exists in the string.

Example

>>> str1=’hello’
>>> str2=’world’
>>> print(‘h’ in str1)
True
>>> print(‘W’ in str2)
False

String Slicing

A slice of string is nothing but a substring. A chunk of characters can be extracted by using the slice syntax. Specify the start index and the end index, separated by a colon, to return a part of the string.

Example

>>>b = “Hello, World!”
>>>print(b[2:5]) #this code segment will display ‘llo’

Negative Indexing

Negative indexes are used start the slice from the end of the string: 

Example

>>>b = “Hello, World!”
>>>print(b[-5:-2]) # this code segment will display ‘orl’

Built-in String methods

MethodDescriptionExample
isalpha()Returns true if the string contains only alphabets>>>str=’Good’
>>>print(str.isalpha())
True
isdigit()Returns true if the string contains only digits>>>str=’123456’
>>>print(str.isdigit())
True
lower()Converts all the uppercase letters in the string to lower case>>>str=’GOOD’
>>>print(str.lower())
good
upper()Converts all the lowercase letters in the string to uppercase>>>str=’good’
>>>print(str.lower())
GOOD
islower()Returns true if all the letters in the string are lowercase>>>str=’good’
>>>print(str.islower())
True
isupper()Returns true if all the letters in the string are uppercase>>>str=’GOOD’
>>>print(str.isupper())
True
lstrip() / lstrip(chars)Returns the string after removing spaces from the left side of the string>>>str=” hello world”
>>>print(str.lstrip())
“hello world”
>>>str=”hello world”
>>>print(str.lstrip(he))
“llo world”
rstrip() / rstrip(chars)Returns the string after removing spaces from the right side of the string>>>str=”hello world ”
>>>print(str.lstrip())
“hello world”
>>>str=”computers”
>>>print(str.lstrip(rs))
“compute”
isspace()Returns true if the string contains only spaces otherwise false>>>str=” “
>>>print(str.isspace())
True
>>>str=” computer “
>>>print(str.isspace())
False
istitle()If the first letter of each word in the string are uppercase, this functions returns true otherwise false.>>>str=”Revision Tour In Pyhton“
>>>print(str.istitle())
True
join(sequence)This method takes all items in an iterable and joins them into one string using a specified character>>>str=”12345”
>>>x=“-“.join(str())
>>>print(x)
1-2-3-4-5
swapcase()Make the lower case letters upper case and the upper case letters lower case>>>str=”Revision Tour In Pyhton“
>>>print(str.swapcase())
rEVISION tOUR iN pYHTON
partition(separator)This method searches for a specified string, and splits the string into a tuple containing three elements.>>>str = “I could eat bananas all day”
>>>x=str.partition(“bananas”)
>>>print(x)
(‘I could eat’, ‘bananas’, ‘all day’)
ord()Returns the ASCII value of the specified character>>>ch=’b’
>>>ord(ch)
98
chr()Returns the character represented by given ASCII value.>>>n=66
>>>chr(n)
B

Number Of Visitors

0238259