# Python Questions and Answers – Regular Expressions – 4

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Regular Expressions – 4”.

1. What will be the output of the following Python code?

`re.split(r'(a)(t)', 'Maths is a difficult subject')`

a) [‘M a t h s i s a d i f f i c u l t s u b j e c t’]
b) [‘Maths’, ‘is’, ‘a’, ‘difficult’, ‘subject’]
c) ‘Maths is a difficult subject’
d) [‘M’, ‘a’, ‘t’, ‘hs is a difficult subject’]

Explanation: The code shown above demonstrates the use of the function re.match. The first argument of this function specifies the pattern. Since the pattern contains groups, those groups are incorporated in the resultant list as well. Hence the output of the code shown above is [‘M’, ‘a’, ‘t’, ‘hs is a difficult subject’].

2. The output of the following two Python codes are the same.

```CODE 1
>>> re.split(r'(a)(t)', 'The night sky')
CODE 2
>>> re.split(r'\s+', 'The night sky')```

a) True
b) False

Explanation: The output of the first code is: [‘The night sky’] whereas the output of the second code is:[‘The’, ‘night’, ‘sky’]. Clearly, the outputs of the two codes are different. Hence the statement given above is a false one.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. What will be the output of the following Python code?

```import re
s = 'abc123 xyz666 lmn-11 def77'
re.sub(r'\b([a-z]+)(\d+)', r'\2\1:', s)```

a) ‘123abc: 666xyz: lmn-11 77def:’
b) ‘77def: lmn-11: 666xyz: 123abc’
c) ‘abc123:’, ‘xyz666:’, ‘lmn-11:’, ‘def77:’
d) ‘abc123: xyz666: lmn-11: def77’

Explanation: The function re.sub returns a string produced by replacing every non overlapping occurrence of the first argument with the second argument in the third argument. Hence the output is: ‘123abc: 666xyz: lmn-11 77def:’

4. What will be the output of the following Python code?

`re.subn('A', 'X', 'AAAAAA', count=4)`

a) ‘XXXXAA, 4’
b) (‘AAAAAA’, 4)
c) (‘XXXXAA’, 4)
d) ‘AAAAAA, 4’

Explanation: The line of code shown above demonstrates the function re.subn. This function is very similar to the function re.sub except that in the former, a tuple is returned instead of a string. The output of the code shown above is: (‘XXXXAA’, 4).

5. What will be the output of the following Python code?

`n = re.sub(r'\w+', 'Hello', 'Cats and dogs')`

a)

```    Hello
Hello
Hello```

b) ‘Hello Hello Hello’
c) [‘Hello’, ‘Hello’, ‘Hello’]
d) (‘Hello’, ‘Hello’, ‘Hello’)

Explanation: The code shown above demonstrates the function re.sub. Since the string given as an argument consists of three words. The output of the code is: ‘Hello Hello Hello’. Had the string consisted of 4 words, the output would be: ‘Hello Hello Hello Hello’

6. What will be the output of the following Python code?

```w = re.compile('[A-Za-z]+')
w.findall('It will rain today')```

a) ‘It will rain today’
b) (‘It will rain today’)
c) [‘It will rain today’]
d) [‘It’, ‘will’, ‘rain’, ‘today’]

Explanation: The code shown above demonstrates the function re.findall. Since all the words in the string match the criteria, the output of the code is: [‘It’, ‘will’, ‘rain’, ‘today’].

7. In the functions re.search.start(group) and re.search.end(group), if the argument groups not specified, it defaults to __________
a) Zero
b) None
c) One
d) Error

Explanation: In the functions re.search.start(group) and re.search.end(group), if the argument groups not specified, it defaults to Zero.

8. What will be the output of the following Python code?

`re.split(r'\s+', 'Chrome is better than explorer', maxspilt=3)`

a) [‘Chrome’, ‘is’, ‘better’, ‘than’, ‘explorer’]
b) [‘Chrome’, ‘is’, ‘better’, ‘than explorer’]
c) (‘Chrome’, ‘is’, ‘better’, ‘than explorer’)
d) ‘Chrome is better’ ‘than explorer’

Explanation: The code shown above demonstrates the use of the function re.split, including the use of maxsplit. Since maxsplit is equal to 3, the output of the code shown above is:[‘Chrome’, ‘is’, ‘better’, ‘than explorer’]

9. What will be the output of the following Python code?

```a=re.compile('[0-9]+')
a.findall('7 apples and 3 mangoes')```

a) [‘apples’ ‘and’ ‘mangoes’]
b) (7, 3)
c) [‘7’, ‘3’]
d) Error

Explanation: The code shown above demonstrates the use of the functions re.compile and re.findall. Since we have specified in the code that only digits from 0-9 be found, hence the output of this code is: [‘7’, ‘3’].

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]