String Flashcards

complete String- with functions

1
Q

str1=’Python’

str1[0]

> > >

A

P

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

str1= ‘Python’
str1[2+3]
»>

A

n

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Slicing in string and its Syntax?

A

Extracting a substring from a string
Syntax: string[start:end:step]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

str1=’python’
print(str1[-5:-1])
Output

A

YTHO

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

str1=’python’

print(str1[-1:-4])
Output

A

’’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

str1=’python’
print(str1[:-5:-1])

A

NOHT

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Mutable Object

A

If the value of an object can be changed after it is created, It is called mutable object
Example
Lists, Set and dictionaries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Immutable Object

A

If the value of an object cannot be changed after it is created, it is called immutable object
Example
Numeric, String and Tuple

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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’

A

TypeError

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Concatenation
Operator : +
»> str1 = “Python”
»> str2 = “Programming”
»> str1 + str2

A

‘PythonProgramming’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Repetition
Use : To repeat the given string multiple times.
Repetition operator : *
»> str1 = “Hello “
»> str1*5

A

‘Hello Hello Hello Hello Hello’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A

True
True
False
True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

String Functions

len()
Returns the length of the given string
»> str1 = ‘Hello World!’
»> len(str1)

A

12

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

capitalize()
converts the first character of a string to capital
(uppercase) letter and rest in lowercase.
str1 = “python Programming for
11th”
str1.capitalize()

A

‘Python programming for 11th’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

title()
converts the first letter of every word in the string
in uppercase and rest in lowercase
»> str1 = “python ProGramming
for 11th”
»> str1.title()

A

‘Python Programming For 11th’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

lower()
Returns the string with all uppercase letters
converted to lowercase
»> str1 = “PYTHON PROGRAMMING for
11th”
»> str1.lower()

A

‘python programming for 11th’

17
Q

upper()
Returns the string with all lowercase letters
converted to uppercase
»> str1 = “python programming
for 11th”
»> str1.upper()

A

‘PYTHON PROGRAMMING FOR 11TH’

18
Q

count()
Returns number of times a substring occurs in the
given string
»> str1 = “python programming for
11th”
»> str1.count(“p”)
2
»> str1.count(“pyth”)

19
Q

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’)

20
Q

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’)

A

ValueError: substring not found

21
Q

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()

22
Q

isalpha()
Returns True if all characters in the string are
alphabets, Otherwise, It returns False
»> ‘Python’.isalpha()
True
»> ‘Python 123’.isalpha()

23
Q

isdigit()
returns True if all characters in the string are
digits, Otherwise, It returns False
»> ‘1234’.isdigit()
True
»> ‘123 567’.isdigit()

24
Q

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()

25
**islower(**) returns True if the string has letters all of them are in lower case and otherwise False. >>> str1 = 'hello world!' >>> str1.islower() True >>> str1 = 'hello 1234' >>> str1.islower() True >>> str1 = 'hello ??' >>> str1.islower()
True
26
**isupper()** returns True if the string has letters all of them are in upper case and otherwise False. >>> str1 = 'HELLO WORLD!' >>> str1.isupper() True >>> str1 = 'HELLO 1234' >>> str1.isupper() True >>> str1 = 'HELLO ??' >>> str1.isupper()
True
27
**strip()** Returns the string after removing the whitespaces both on the left and the right of the string >>> str1 = ' Hello World! ' >>> str1.strip()
'Hello World!'
28
**lstrip()** Returns the string after removing the whitespaces only on the left of the string >>> str1 = ' Hello World! ' >>> str1.lstrip()
'Hello World! '
29
**rstrip()** Returns the string after removing the whitespaces only on the right of the string >>> str1 = ' Hello World! ' >>> str1.rstrip()
' Hello World!'
30
**replace(oldstr,newstr)** used to replace a particular substring in a string with another substring >>> str1 = 'Hello World!' >>> str1.replace('o','*')
'Hell* W*rld!'
31
**partition()** The partition() function is used to split a string into three parts based on a specified separator. >>> str1 = 'India is a Great Country' >>> str1.partition('is') ('India ', 'is', ' a Great Country') >>> str1.partition('are')
('India is a Great Country',' ',' ')
32
**split()** Returns a list of words delimited by the specified substring. If no delimiter is given then words are separated by space. >>> str1 = 'India is a Great Country' >>> str1.split() ['India','is','a','Great', 'Country'] >>> str1 = 'India is a Great Country' >>> str1.split('a')
['Indi', ' is ', ' Gre', 't Country']
33
**startswith()** startswith() function is used to check whether a given string starts with a particular substring. endswith() endswith() function is used to check whether a given string ends with a particular substring. str = "Python Programming Language" print(str.startswith("Pyt")) print(str.startswith("Pyth")) print(str.endswith("age")) print(str.endswith("uage")) print(str.startswith("Pyts"))
Output True True True True False
34
**join()** str.join(sequence) returns a string in which the string elements of sequence have been joined by str separator. if few of the members of the sequence are not string, error is generated. >>> '*-*'.join('Python') 'P*-*y*-*t*-*h*-*o*-*n' >>> '*- *'.join(['Ajay','Abhay','Alok']) 'Ajay*-*Abhay*-*Alok' >>> '*- *'.join(['Ajay','Abhay',123])
TypeError: sequence item 2: expected str instance, int found