Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / Python / Python Fundamentals – String Manipulation

Python Fundamentals – String Manipulation

February 21, 2020 by James Palmer

Python Fundamentals – String Manipulation

WHAT IS A STRING?

A string is a sequence of characters. Strings are basically just a bunch of words. Strings are the form of data used in programming for storing and manipulating text, such as words, names and sentences. You can write them in Python using quotes, double quotes, or triple quotes.           .

Single Quote
You can specify .strings using single quotes such as ‘Quote me on this’. All white space i.e. spaces and tabs are preserved as it is.

Double Quotes
Strings in double quotes work exactly the same way as strings in single quotes.
An example is “What’s your name?”.

Triple Quotes
You can specify multi-line strings using tri­ple quotes – (“””). You can use single quotes and double quotes freely within the triple quotes. An example is:
“‘This is a multi-line string. This is the first line.
This is the second line.”What’s your name?,” I asked.
He said “Ram, Raja Ram.”
Docstrings
Python has a feature called documentation strings, usually referred to by its shorter name docstrings. Docstrings are an important tool that you should make use of since it helps to document the program better and makes it easier to understand.

LOGICAL AND PHYSICAL LINE

A physical line is what you see when you write the program. A logical line is what Python sees as a single statement. Python implicitly assumes that each physical line cor­responds to a logical line. An example of a logical line is a statement like print(“Hello World”) – if this was on a line by itself (as you see it in an editor), then this also corresponds to a physical line.
Implicitly, Python encourages the use of a single statement per line which makes code more readable.
If you have a long line of code, you can break it into multiple physical lines by using the backslash. This is referred to as explicit line joining:
For example,

s = 'This is a string. \
This continues the string.' 
print s

This gives the output:

This is a string. This continues the string.

WORKING WITH STRINGS

String is an ordered sequence of letters/characters. They are enclosed in single quotes (“) or double (” “). The quotes are not part of string. They only tell the computer where the string constant begins and ends. They can have any character or sign, including space in them.
‘Hello, World!’ is a string, so-called because it contains a ‘string’ of letters. You (and the interpreter) can identify strings because they are enclosed in quotation marks. If you are not sure what type a value has, the interpreter can tell you.                                                                         t

››› type ('Hello, World!')
‹type 'str'›

It is possible to change one type of value/variable to another type. It is known as type conversion or type casting. The conversion can be done explicitly (programmer specifies the conversions) or implicitly (Interpreter automatically converts the data type).
An empty string contains no characters and has length 0, represented by two quotation marks.
In general, you can’t perform mathematical operations on strings, even if the strings look like numbers, so the following are illegal:

'8'-'2' 'apples'/'easy' 'third'*'first'

The + operator works with strings, but it might not do what you expect; it performs concatenation, which means joining the strings by linking them end-to-end. For example:

first = 'Lakhan' 
second = 'lal' 
print first + second

The output of this program is Lakhanlal.
The * operator also works on strings; it performs repetition. For example, ‘Save’*3 is ‘SaveSaveSave’. If one of the operands is a string, the other has to be an integer.
This use of + and * makes sense by analogy with addition and multiplication. Just as 4*3 is equivalent to 4+4+4, we expect ‘Save’*3 to be the same as ‘Save’+’Save’+’Save’. The plus ( + ) sign is the string concatenation operator, and the asterisk ( * ) is the repetition operator.
A string is a sequence of characters. You can access the characters one at a time with the bracket operator. An individual character in a string is accessed using a subscript (in­dex). The subscript should always be an integer (positive or negative). A subscript starts from 0. To access the first character of the string:

››› str = 'PYTHON'
››› print str[0]
P

The second statement selects character number 0 from str and prints the character P. The expression in brackets is called an index. The index indicates which character in the sequence you want.
Index is an offset from the beginning of the string, and the offset of the first letter is zero. To access the second character of the string:

››› print str[1]
Y

You can use any expression, including variables and operators, as an index, but the value of the index has to be an integer. Otherwise you get an error.

>>> print str[1.5]

TypeError: string indices must be integers
Alternatively, you can use negative indices, which count backward from the end of the string. The expression str[-1] yields the last letter, str[-2] yields the second to last, and so on.
To access the last character of the string:

››› print str[-l]
N

To access the third last character of the string:

››› print str [-3]
H

Strings in Python are stored by storing each character separately in contiguous locations as shown in Figure 15.1.

str P Y T H O N
Positive index 0 1 2 3 4 5
Negative index -6 -5 -4 -3 -2 -1

Figure 15.1 Structure of Python str

The index also called subscript is the numbered position of letter in the string. In Python, indices begin 0 onwards in the forward direction also called positive index, and -1, -2, -3 in the backward direction also called negative index. Positive subscript helps in accessing the string from the beginning. Negative subscript helps in accessing the string from the end.
You can access any character as <stringname>[<index>]. Index or subscript 0 or -ve n(where n is length of the string) displays the first element.
For example : str[0] or str[-6] will display first element ‘P’.
Subscript 1 or -ve (n-1) displays the second element. For example : str[l] or str[-5] will display second element ‘Y’.
The built-in function len() returns the length of a string:

››› str = "PYTHON"
››› len(str)
6

Since length of string variable can be determined using len(<string>) so we can say that:

  • The first character of the string is at index 0 or -length. For example in example given in Figure 15.1, at index 0 or -6 character stored is
  • Second character of the string is at index 1 or -(length-1). For example in example given in Figure 15.1, at index 1 or -(6-1)= -5, character stored is
  • Second last character of the string is at index (length-2) or -2. For example in example given in Figure 15.1, at index (6-2)= 4 or -2, character stored is
  • The last character of the string is at index (length-1) or -1. For example in example given in Figure 15.1, at index (6-1)= 5 or -1, character stored is N

Strings are Immutable
Strings are immutable means that the contents of the string cannot be changed after it is created. Let us understand the concept of immutability with help of an example.

››› greeting = 'Hello, world!'
››› greeting[0] = 'J'

TypeError: object does not support item assignment The “object” in this case is the string and the “item” is the character you tried to assign. Python does not allow the programmer to change a character in a string. As shown in the above example, str has the value ‘Hello, world!’. An attempt to replace ‘H’ in the string by ‘J’ displays a TypeError.
The reason for the error is that strings are immutable, which means you can’t change an existing string. The best you can do is create a new string that is a variation on the origi­nal:

››› greeting = 'Hello, world!'
››› newgreeting = 'J' + greeting[1:]
››› print new_greeting Jello, world!

This example concatenates a new first letter onto a slice (a part of a string specified by a range of indices) of greeting. It has no effect on the original string. We will study string slice latter in this Chapter. Python strings cannot be changed. Assigning to an indexed position in the string results in an error.
You can assign to string another string or an expression that returns a string using assignment. For example,

››› name = 'Teena'
››› name = 'Meena'

Traversing a String
Traversing a string means accessing all the elements of the string one after the other by using the subscript or index. A string can be traversed character-by-character using: for loop or while loop. Example 1 demonstrates string traversal using for loop. In this pro­gram, each character of string “PYTHON” will be accessed one-by-one.

Example 1

# String traversal using for loop 
str = "PYTHON" 
for i in str: 
print i

RUN
››› 
P                                              
Y
T
H
O
N
›››

Figure 15.2 Program for string traversal using for loop

Each time through the for loop, the next character in the string is assigned to the variable str. The loop continues until no character are left.

Example 2

# String traversal using while loop
str = "PYTHON"
i=0
while i ‹ len(str): 
print str[i] 
i = i + 1

RUN
›››
P
Y
T
H
O
N
›››

Figure 15.3 Program for string traversal using while loop

The while loop traverses the string and displays each letter on a line by itself. The loop condition is i < len(str)/ so when i is equal to the length of the string, the condition is false, and the body of the loop is not executed.

Example 3

# Program to find length of entered string 
while True:
s = raw_input('Enter something : ') 
if s == 'quit': 
break
print('Length of the string is', len(s)) 
print('Done')

RUN 
›››
Enter something : Hello PYTHON 
('Length of the : string is', 12) 
Enter something : WELCOME 
('Length of the string is', 7) 
Enter something : quit
Done 
›››

Figure 15.4 Program for finding length of string

In this program, we repeatedly take the user’s input and print the length of each input each time. We are providing a special condition to stop the program by checking if the user input is ‘quit’. We stop the program by breaking out of the loop and reach the end of the program. The length of the input string can be found out using the built-in len() function.

Example 4

# Program to check length of a string 
while True:
s = raw_input('Enter something : ') 
if s == 'quit': 
break
if len(s) ‹ 3:
print('Too small') 
continue
print('Input is of sufficient length')

RUN
›››
Enter something : C 
Too small
Enter something : Ca 
Too small
Enter something : Cat 
Input is of sufficient length 
Enter something : Elephant 
Input is of sufficient length 
Enter something : quit 
›››

Figure 15.5 Program to check length of a string

In this program, we accept input from the user, but we process the input string only if it is at least 3 characters long. So, we use the built-in len function to get the length and if the length is less than 3, we skip the rest of the statements in the block by using the con­tinue statement. Otherwise, the rest of the statements in the loop are executed, doing any kind of processing we want to do here.

STRING SPECIAL OPERATORS

Different string special operators are discussed in the following sections:

Concatenation (+) operator
The + operator joins the text on both sides of the operator. For example,

››› 'Work' + 'Hard'
'WorkHard'

To give a white space between the two words, insert a space before the closing of first string.

››› 'Work ' + 'Hard'
'Work Hard'

Note that the interpreter gives back the string with single quotes whether you create strings with single quotes or double quotes.
The + operator can work with numbers and strings separately for addition and concate-nation respectively. However you cannot combine numbers and strings as operands with a + operator. It will produce an error.

7 + 7 = 14 # (valid) + operator is used as addition
'7' + '7' = '77' # (valid) + operator is used as concatenation
7 + '7'# (invalid) it will produce an error

Replication (*) Operator
Replication * (multiply) gives the multiplication of the two numbers or returns the string repeated that many times. For example,

››› 5*7 35
››› "Save!" * 3 
'Save!Save!Save!'

The * (multiplication) operator when works with number it returns product of two numbers. The * (replication) operator used produces muliple copies of same string. For replication, if one of the operands is a string, the other has to be number, i.e., “string” * number or number * “string”. It cannot work with both operands of string types. It will display an error.

7 * 7 = 49 # (valid) * operator is used as multiplication
'Stop ' * 2 = 'Stop Stop' # (valid) * operator is used as replication
5 * '$' = '$$$$$' # (valid) * operator is used as replication 
'2' * '5'# (invalid) it will produce an error

Membership Operator (in)
Membership operator in returns true if a character exists in the given string, false other-wise. For example,

››› str = "Work Hard"
››› 'W' in str
True
››› 'Work' in str 
True
››› 'work' in str
False # because w of work is in small case
››› 'We' in str
False

Membership Operator (not in)
The not in membership returns true if a character does not exist in the given string. For example,

››› str = "Work Hard"
››› 'W' not in str 
False
››› 'Word' not in str
True

Comparison Operators
All the comparison operators (<,<=, > >=, <> ==) are applied to strings also. The com­parisons using these operators are character by character comparison rules for ASCII and Unicode. ASCII values of numbers 0 to 9 are from 48 to 57, uppercase (A to Z) ASCII values are 65 to 90 and lowercase (a to z) ASCII values are form 97 to 122. Python has the two boolean values True and False. Many comparison operators return True and False as discussed in Figure 15.6.

Comparisons Result Reason
“abc” == “abc” True Because letter case are same.
“a” == “A” False Because letter case is different.
“a” <> “A” True Because letter case is different.
“abc” > “ABC” True Because uppercase letters are considered smaller than the lowercase letters due to their ASCII val­ues.
‘123’ > ‘456’ False Because ASCII value of 123 is lower than 456.

Figure 15.6 Working of comparison operators with strings

Python provides a couple built-in functions that allow us to switch back and forth between characters and the numeric values used to represent them in strings. The ord function returns the numeric (“ordinal”) code of a single-character string, while chr goes the other direction. Here are some interactive examples:

››› ord("a")
97
››› ord("A")
65
››› chr(97)
'a'
››› chr(90)
'Z'

STRING SLICES

You can retrieve a slice (substring) of a string with a slice operation. The slicing opera­tion is used by specifying the name of the sequence followed by an optional pair of numbers separated by a colon within square brackets. Note that this is very similar to the indexing operation you have been using till now. Remember the numbers are optional but the colon isn’t. Characters are accessible using their position in the string which has to be enclosed into brackets following the string. The position number can be positive or negative, which means starting at the beginning or the end of the string. Substrings are extracted by providing an interval of the start and end position separated by a colon. Both positions are optional which means either to start at the beginning of the string or to extract the substring until the end. When accessing characters, it is for­bidden to access position that does not exist, whereas during substring extraction, the longest possible string is extracted. Figure 15.7 summarizes the arrangement of slicing on a string ‘SWAROOP’.

str S W A R O O P
Positive index 0 1 2 3 4 5 6
Negative index -7 -6 -5 -4 -3 -2 -1

Figure 15.7 Structure of SWAROOP str

Example 5

# Slicing on a string 
name = 'SWAROOP' 
print('characters 1 to 3 is', name[1:3])
print('characters 2 to end is', name[2:])
print('characters 1 to -1 is', name[1:-1] )
print('characters 0 to -1 is', name[:-1])
print('characters -7 to -1 is', name[-7:-1] )
print('characters -5 to -3 is', name[-5:-3])
print('characters start to end is', name[:])
RUN 
››› 
('characters 1 to 3 is', 'WA' )
('characters 2 to end is', 'AROOP')
('characters 1 to -1 is', 'WAROO')
('characters 0 to -1 is', 'SWAROO')
('characters - 7 to -1 is', 'SWAROO')
('characters -5 to -3 is', 'AR' )
('characters start to end is', 'SWAROOP') ’
›››

Figure 15.8 Program for Slicing on a string

Remember the following points while accessing elements in the strings using subscripts:

  • The first number (before the colon) in the slicing operation refers to the position from where the slice starts and the second number (after the colon) indicates where the slice will stop.
  • If the first number is not specified, Python will start at the beginning of the sequence.
  • If the second number is left out, Python will stop at the end of the sequence.
  • Note that the slice returned starts at the start position and will end just before the end position, i.e., the start position is included but the end position is excluded from the sequence slice. Thus, name[l:3] returns a slice of the sequence starting at position 1, includes position 2 but stops at position 3 and therefore a slice of two items is returned.
  • Omitting both the indices, directs the python interpreter to extract the entire string starting from 0 till the last index, i.e., name[:] returns a copy of the whole sequence.
  • Positive subscript helps in accessing the string from the beginning. You can also do slicing with negative positions. Negative numbers are used for positions from the end of the sequence i.e., count backward from the end of the string. For example, name[:-l] will return a slice of the sequence which excludes the last item of the sequence but contains everything else.
  • If the first index is greater than or equal to the second, the result is an empty string, represented by two quotation marks.
  • Note that -0 is really the same as 0, so it does not count from the right.

STRING FUNCTIONS AND METHODS

Python provides built-in functions and methods that allow us to do string manipulation. Every string object that you create in Python is actually an instance of String class. The string manipulation methods that are discussed below use the following syntax:

‹stringObject>.cmethod name›()

len()
len() is a built-in function that returns the number of characters in a string.

››› fruit = 'apple'
››› len(fruit)
5

An empty string contains no characters and has length 0, but other than that, it is the same as any other string. A string with length 1 represents a character in Python.

capitalize()
The method capitalize)) returns a copy of the string with only its first character capitalized.

››› str = "this is string"
››› str.capitalize()
This is string

find()
The find() method is used to locate the position of the given substring within the string; find returns -1 if it is unsuccessful in finding the substring.

find(str, beg=0 end=len(string))
››› str= "This is a simple sentence"
››› str.find('tan')
-1

It returns -1 because substring is not found in the given string. On omitting the start parameter the function starts the searching process from the very beginning.

››› str.find('ten')
20
››› str.find('is',0,4)
2
››› str.find('is',4,15)
5

isalnum()
Returns true if string contains only letters and digit, i.e., all characters are alphanumeric and false otherwise if the string contains any special character like _ , @,#,* including space.

››› str1='Work Hard'
››› strl.isalnum()
False

because it has a space which is a special character.

››› str2='Work'
››› str2.isalnum()
True

››› str3 = 'Work1'
››› str3 .isalnum() 
True

››› str4 ='1234'
››› str4.isalnum()
True

isalpha()
Returns True if the string contains only letters, otherwise returns False.

››› str2='Work' 
››› str2.isalpha() 
True

››› str3='Workl' 
››› str3.isalpha() 
False

›str4='1234' 
››› str4.isalpha() 
False

isdigit()
Returns true if string contains only digits and false otherwise.

››› str2='Work'
››› str2.isdigit()
False

››› str3 ='Work1'
››› str3.isdigit()
False

››› str4='1234'
››› str4.isdigit()
True

lower()
Converts all uppercase letters in string to lowercase.

››› 'Save Water'.lower() 
save water

islower()
Returns True if the string is in lowercase.

››› str1='Work Hard'
››› str1.islower()
False

››› str2='hello'
››› str2.islower()
True

››› str3='ROHAN
››› str3.islower()
False

isupper()
Return true if string is in upper case

››› Str1='Work Hard'
››› Str1.isupper()
False

››› Str2='hello'
››› Str2.isupper()
False

››› Str3='ROHAN'
››› Str3.isupper()
True

Upper()
Return exact copy of the string with all letters in uppercase

››› 'save water'.upper()
SAVE WAETR

lstrip()
Remove all the leading(left side) whitespace in string

››› str = "save water"
››› str.lstrip()
'save water'

if a string is passed as an argument to the lstrip() function, it remove those characters from the left of the string

››› str='ABC International'
ABC International

››› str.lstrip('A')
'BC International'

››› str.lstrip('ABC')
'International'

rstrip()
Remove all trailing(right side) whitespace of string

››› str2 = "Hello World"
››› str2 .rstrip() 
'Hello World'

If a string is passed as an argument to the rstripO function, it removes those characters from the right of the string.

››› str = "Television"
››› str.rstrip('vision')
'Tele'

isspace()
Returns true if string contains only whitespace characters and false otherwise.

››› ' '.isspace()
True
››› ''.isspace()
False
››› str='w'
››› print str.isspace()
False

It will return false if the string contains even one character.

istitle()
Checks whether string is title-case meaning all words are capitalized. Returns true if string is properly “titlecased” and false otherwise.

››› 'This Is A Title'.istitle()
True
››› 'This is A title'.istitle ()
False

replace(old,new)
Replace all occurrences of old substring in a string with new substring.

››› str = 'This is just a simple string.'
››› str.replace ('simple', 'short')
'This is just a short string.'

››› 'This is a test'.replace('is', 'eez')
'Theez eez a test'

The replace() works like a search and replace feature of a wordprocessing.

split()
Splits string according to delimiter str (space if not provided) and returns list of sub-strings, i.e., it splits a string into a list of “words”.

››› s1 = "Work is Worship"
››› words = s1.split ()
››› words
['Work', 'is', 'Worship']

join()
The join() method concatenates strings from a list of strings to form a single string. Ele-ments can be joined using any separator.

››› str1 = ('Raja', 'Ram', 'Mohan', 'Rai')
››› str1
('Raja', 'Ram', 'Mohan', 'Rai')
››› str = ' '
››› str.join(str1)
'Raja Ram Mohan Rai'

swapcase()
Returns a copy of string with lowercase letters turn into uppercase and vice versa.

››› str='COMPUTER'
››› print str.swapcase() 
computer

››› str='computer'
››› print str.swapcase()
COMPUTER

partition()
This function partitions the strings at the first occurrence of separator, and returns the strings partition in three parts, i.e., before the separator (head), the separator itself, and the part after the separator (tail). If the separator is not found, it returns the string itself, followed by two empty strings.

››› str = "The Golden Star"
››› str.partition('Gold')
('The ', 'Gold', 'en Star')

››› str.partition('ne')
('The Golden Star', ", ")

››› str.partition('e')
('Th', 'e', ' Golden Star')

SOLVED EXERCISE

Question 1.
What are strings?

Answer:
A string is a sequence of characters. Strings are basically just a bunch of words. Strings are the form of data used in programming for storing and manipulating text, such as words, names and sentences. You can write them in Python using quotes, double quotes, or triple quotes.

Question 2.
A palindrome is a word that is spelled the same backward and forward. Write a program that checks that the word is palindrome or not.

Answer:

def reverse(text):
 return text[::-1]
 def is_palindrome(text):
 return text == reverse(text)
 something = input('Enter text: ')
 if (is_palindrome(something)):
 print("Yes, it is a palindrome")
 else
 print("No, it is not a palindrome")

RUN
 ›››
 Enter text: 'sir'
 No, it is not a palindrome
 ›››
 =============== RESTART =====================
 ›››
 Enter text: 'madam' .
 Yes, it is a palindrome
 ›››

Figure 1 Program for Q2

Question 3.
Write a program that finds first character, last character and middle characters from entered string.

Answer:

def first(word):
 return word[0]
 def last(word):
 return word[-1]
 def middle(word):
 return word[l:-l]
 word = input('Enter text: ')
 print "First character of the string : ", word[0]
 print "Last character of the string : ",word[-1]
 print "Middle characters(s) of the string : ", word[1:-1]

RUN
 ›››
 Enter text: 'sir'
 First character of the string : s
 Last charcter of the string : r
 Middle characters(s) of the string : i
 ›››
=============== RESTART =====================
 Enter text: 'SHASHI'
 First character of the string : S
 Last character of the string : I
 Middle characters(s) of the string : HASH
 ›››

Figure 2 Program for Q3

Question 4.
Find the output of the following program segments:

(i) ››› strl = 'Computer World'
 ››› len(strl)
(ii) word = 'banana'
 count = 0
 for letter in word:
 if letter == 'a':
 count = count + 1
 print count
(iii) ››› word = 'banana'
 ››› new_word = word.upper()
 ››› print new_word

Answer:
(i) 14
(ii) 3
(iii) BANANA

Question 5.
What is the significance of triple quotes?

Answer:
You can specify multi-line strings using triple quotes – (“””). You can use single quotes and double quotes freely within the triple quotes. An example is:
‘”This is a multi-line string. This is the first line.
This is the second line.”What,s your name?,” I asked.
He said “Ram, Raja Ram.”

Question 6.
Differentiate between physical line and a logical line.

Answer:
A physical line is what you see when you write the program. A logical line is what Python sees as a single statement. Python implicitly assumes that each physical line corresponds to a logical line.

Question 7.
What is difference between lstrip() and rstripO?

Answer:
lstrip() removes all leading (left side) whitespace in string.
rstrip() removes all trailing (right side) whitespace of string.

Question 8.
Explain membership operator in with example.

Answer:
Membership operator in returns true if a character exists in the given string, false otherwise. For example,

››› str = "Work Hard"
 ››› 'W' in str
 True
 ››› 'Work' in str
 True
 ››› 'work' in str
 False # because w of work is in small case
 ››› 'We' in str
 False

Question 9.
What do you mean by ‘strings are immutable’?

Answer:
Strings are immutable means that the contents of the string cannot be changed after it is created.

Question 10.
What is the function of slicing operation?

Answer:
You can retrieve a slice (substring) of a string with a slice operation. The slicing operation is used by specifying the name of the sequence followed by an optional pair of numbers separated by a colon, within square brackets.

Question 11.
What is the use of Docstrings?

Answer:
Python has a feature called documentation strings, usually referred to by its shorter name docstrings. Docstrings are an important tool that you should make use of since it helps to document the program better and makes it easier to understand.

Question 12.
The quotes are not part of string. Comment.

Answer:
The quotes are not part of string. They only tell the computer where the string con-stant begins and ends. They can have any character or sign, including space in them.

Question 13.
How empty strings are represented in Python?

Answer:
An empty string contains no characters and has length 0, represented by two quota-tion marks in Python.

Question 14.
What is the difference between (+) and (*) operators?

Answer:
The plus (+) sign is the string concatenation operator, and the asterisk (*) is the repe-tition operator. You can concatenate strings with the “+” operator and create multiple concatenated copies of a string with the operator.
The use of + and * makes sense by analogy with addition and multiplication. Just as 4*3 is equivalent to 4+4+4, we expect ‘Save’*3 to be the same as ‘Save’+’Save’+’Save’.

Question 15.
Differentiate between ord() and chr() functions?

Answer:
ord() function returns the ASCII value of a character and ord() function requires single character only.
chr() function takes ASCII value in integer form and returns the character corre-sponding to the ASCII value.

Question 16.
Write short note on subscript.

Answer:
A string is a sequence of characters. You can access the characters one at a time with the bracket operator. An individual character in a string is accessed using a subscript (index). The subscript should always be an integer (positive or negative). A subscript starts from 0.

Question 17.
What do you mean by positive and negative indices?

Answer:
In Python, indices begin 0 onwards in the forward direction also called positive index, and -1, -2, -3 in the backward direction also called negative index. Positive subscript helps in accessing the string from the beginning. Negative subscript helps in accessing the string from the end.

Question 18.
‘String indices must be an integer’. Comment.

Answer:
You can use any expression, including variables and operators, as an index, but the value of the index has to be an integer. Otherwise you get an error. You can use neg-ative indices, which count backward from the end of the string.

Question 19.
What do you mean by traversing a string?

Answer:
Traversing a string means accessing all the elements of the string one after the other by using the subscript or index.

Multiple choice questions

Question 1.
Converts all uppercase letters in string to lowercase. .

  • islower()
  • swapcase()
  • lower()
  • None of the above

Answer:
lower()

Question 2.
The method __returns a copy of the string with only its first character capitalized.

  • upper()
  • capitalize()
  • swapcase()
  • None of the above

Answer:
capitalize()

Question 3.
Returns true if string contains only letters and digit.

  • isalnum()
  • isdigit()
  • isalpha()
  • None of the above

Answer:
isalnum()

Question 4.
Replication (*) means:

  • It gives the multiplication of the two numbers
  • It returns the string repeated that many times.
  • Both (a) and (b)
  • None of the above

Answer:
Both (a) and (b)

Question 5.
The find() method returns -1 if it is:

  • Unsuccessful in finding the substring.
  • Successful in finding the substring.
  • Both (a) and (b)
  • None of the above

Answer:
Unsuccessful in finding the substring.

  • Python Fundamentals

Related

Filed Under: Python

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2023 · News Pro Theme on Genesis Framework · WordPress · Log in