String Flashcards
complete String- with functions
str1=’Python’
str1[0]
> > >
P
str1= ‘Python’
str1[2+3]
»>
n
What is Slicing in string and its Syntax?
Extracting a substring from a string
Syntax: string[start:end:step]
str1=’python’
print(str1[-5:-1])
Output
YTHO
str1=’python’
print(str1[-1:-4])
Output
’’
str1=’python’
print(str1[:-5:-1])
NOHT
Mutable Object
If the value of an object can be changed after it is created, It is called mutable object
Example
Lists, Set and dictionaries.
Immutable Object
If the value of an object cannot be changed after it is created, it is called immutable object
Example
Numeric, String and Tuple
String is Immutable
A string is an immutable data type. It means that the value (content) of a string object cannot be changed
after it has been created. An attempt to do this would lead to an error.
Example
»> str1 = ‘Python’
»> str1[1]=’Y’
TypeError
Concatenation
Operator : +
»> str1 = “Python”
»> str2 = “Programming”
»> str1 + str2
‘PythonProgramming’
Repetition
Use : To repeat the given string multiple times.
Repetition operator : *
»> str1 = “Hello “
»> str1*5
‘Hello Hello Hello Hello Hello’
Membership
Membership is the process of checking whether a particular character or substring is present in a sequence
or not. It is done using the ‘in’ and ‘not in’ operators.
Example
»> str1 = “Programming in Python”
> > > “Prog” in str1“ming in”
in str1“Pyth “ in str1“Pyth “
not in str1
True
True
False
True
String Functions
len()
Returns the length of the given string
»> str1 = ‘Hello World!’
»> len(str1)
12
capitalize()
converts the first character of a string to capital
(uppercase) letter and rest in lowercase.
str1 = “python Programming for
11th”
str1.capitalize()
‘Python programming for 11th’
title()
converts the first letter of every word in the string
in uppercase and rest in lowercase
»> str1 = “python ProGramming
for 11th”
»> str1.title()
‘Python Programming For 11th’
lower()
Returns the string with all uppercase letters
converted to lowercase
»> str1 = “PYTHON PROGRAMMING for
11th”
»> str1.lower()
‘python programming for 11th’
upper()
Returns the string with all lowercase letters
converted to uppercase
»> str1 = “python programming
for 11th”
»> str1.upper()
‘PYTHON PROGRAMMING FOR 11TH’
count()
Returns number of times a substring occurs in the
given string
»> str1 = “python programming for
11th”
»> str1.count(“p”)
2
»> str1.count(“pyth”)
1
find()
Returns the index of the first occurrence of
substring in the given string. If the substring is
not found, it returns -1
»> str1 = “python programming
for 11th”
»> str1.find(‘r’)
8
»> str1.find(‘u’)
-1
index()
Same as find() but raises an exception if the
substring is not present in the given string
»> str1 = “python programming for
11th”
»> str1.index(‘r’)
8
»> str1.index(‘u’)
ValueError: substring not found
isalnum()
The isalnum() method returns True if all
characters in the string are alphanumeric (either
alphabets or numbers). If not, it returns False.
»> str1 = ‘HelloWorld’
»> str1.isalnum()
True
»> str1 = ‘HelloWorld2’
»> str1.isalnum()
True
isalpha()
Returns True if all characters in the string are
alphabets, Otherwise, It returns False
»> ‘Python’.isalpha()
True
»> ‘Python 123’.isalpha()
False
isdigit()
returns True if all characters in the string are
digits, Otherwise, It returns False
»> ‘1234’.isdigit()
True
»> ‘123 567’.isdigit()
False
isspace()
Returns True if has characters and all of them are
white spaces (blank, tab, newline)
»> str1 = ‘ \n \t’
»> str1.isspace()
True
»> str1 = ‘Hello \n’
»> str1.isspace()
False