0% found this document useful (0 votes)
14 views4 pages

Coding Snippets with Outputs

This document contains coding exercises and their outputs related to string manipulation in Python. It includes various snippets demonstrating syntax errors, string slicing, replacing characters, and string concatenation. Each code snippet is followed by its expected output, providing a practical guide for students learning Python programming.

Uploaded by

paaaa4978
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Coding Snippets with Outputs

This document contains coding exercises and their outputs related to string manipulation in Python. It includes various snippets demonstrating syntax errors, string slicing, replacing characters, and string concatenation. Each code snippet is followed by its expected output, providing a practical guide for students learning Python programming.

Uploaded by

paaaa4978
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

12 – COMPUTER SCIENCE

LESSON – 8

CODING WITH ANSWERS

WRITE THE OUTPUT OF THE FOLLOWING SNIPPETS. / பின்வரும் குறிமுறைகளின் வெளியீட்டை எழுதுக.
1) >>> print (‘I am the best student’)
I am the best student
2) >>> print ('Greater Chennai Corporation's student')
ERROR! (SyntaxError: invalid syntax )
3) str1 = "Chennai Schools"
str1[7] = "-"
ERROR! (Type error)
4) str1 = "Chennai Schools"
str1= "Delhi Schools"
print(str1)
Delhi Schools
5) s="How are you?"
print([Link]("o","e"))
Hew are yeu?
6) s="How are you?"
[Link]("o","e")
print(s)
How are you?
7) str1=" Welcome to "
str1+="Learn Python"
print (str1*3)
Welcome to Learn Python Welcome to Learn Python Welcome to Learn Python
8) str1 = "Welcome to Government School"
print (str1[11:21])
print (str1[11:21:4])
print (str1[11:21:3])
print (str1[11:21:2])
print (str1[::3])
print (str1[::-3])

..1..
Government
Grn
Gemt
Gvrmn
Wceoore hl
lh erooecW
9) str1="TamilNadu"
print(str1[::-1]
udaNlimaT
10) str1="THIRUKKURAL"
print (str1[5])
print (str1[:5])
print (str1[5:])
K
THIRU
KKURAL
11) str1="COMPUTER"
index=0
for i in str1:
print (str1[:index+1])
index+=1
C
CO
COM
COMP
COMPU
COMPUT
COMPUTE
COMPUTER

12) str1="COMPUTER"
index=len(str1)
for i in str1:
print (str1[:index])
index-=1

..2..
COMPUTER
COMPUTE
COMPUT
COMPU
COMP
COM
CO
C
13) city="chennai"
print([Link]())
print(city)
print([Link]().swapcase())
Chennai
chennai
cHENNAI
14) str1="tech easy"
str2="Tech"
if str2 in str1:
print ("Found")
else:
print ("Not Found")
str4="tech"
if str4 in str1:
print ("Found")
else:
print ("Not Found")
Not Found
Found
15) str1=' * '
i=5
while i>=1:
print (str1*i)
i-=1
* * * * *
* * * *
* * *
* *
*

..3..
16) str1="ABCDEFGH"
str2="ate"
for i in str1:
print ((i+str2),end='\t')
Aate Bate Cate Date Eate Fate Gate Hate
17) str1 = "welcome"
str2 = "to school"
str3=str1[:2]+str2[len(str2)-2:]
print(str3)
weol
18) str1 = "welcome"
str2 = "to school"
str3=str1[3:]+str2[:len(str2)]
print(str3)
cometo school
19) str1='education department'
print([Link]())
print([Link]())
print([Link]())
print(str1)
Education department
Education Department
EDUCATION DEPARTMENT
education department
20) str1="Welcome "
str1 + " by Tech Easy"
print(str1)
str1 += " by Tech Easy"
print(str1)
Welcome
Welcome by Tech Easy
21) str1="Tech Easy"
The positive index of the character ‘h’ is 3
The negative index of the character ‘h’ is -6

..4..

Common questions

Powered by AI

The loop iterates over each character in `str1`, concatenates it with `str2`, and then prints the result followed by a tab space. This results in a series of new strings: 'Aate', 'Bate', 'Cate', etc., demonstrating string concatenation within loop iterations .

In string slicing `str1[11:21:3]`, indexing begins at the 11th character up to, but not including, the 21st character, with a step of 3. Starting from index 11 (G), characters are selected every 3 indices, resulting in the selection of 'G', followed by 'r' from index 14, and 'n' from index 17, thus yielding the output 'Grn' .

The operation `str1 += " by Tech Easy"` modifies `str1` in place by extending the existing string, due to the `+=` operator being defined to handle in-place string concatenation. Meanwhile, `str1 = "Delhi Schools"` reassigns the variable to a new string object, entirely replacing the original assignment with a new reference, not affecting the original string object .

Executing `print(city.capitalize())` will output `Chennai`, but `print(city)` will output `chennai`. The `capitalize()` method returns a new string with the first character in uppercase and the rest in lowercase without modifying the original string `city`, which remains unchanged .

The loop forms a descending pattern of asterisk characters, where each iteration multiplies `str1` by `i`, decreasing the count of asterisks by one each time. Starting from 5 asterisks down to 1, each line shows one less asterisk than the previous, highlighting the loop's decrement operation and string multiplication .

Strings in Python are immutable, meaning that methods like `replace()` do not alter the original string unless the result is assigned back to a variable. In `s.replace('o', 'e')`, the method creates a new string with replacements, but since the resulting string is not assigned to a variable, `print(s)` continues to output the original string without the changes .

The output of `print(str1[::-1])` will be `udaNlimaT`, which is the reverse of the string ‘TamilNadu’. The slice `[::-1]` is used for reversing a string by starting from the end to the beginning .

The assignment `str1 = "Delhi Schools"` does not modify the existing string object but instead points the variable `str1` to a new string object ‘Delhi Schools’. Variables in Python are pointers to objects, and replacing their assigned value points the variable to a different immutable string object, which is allowed .

The slice operation `str1[5:]` begins at index 5, corresponding to the character 'K', and includes all subsequent characters to the end of the string. Consequently, it outputs 'KKURAL', effectively demonstrating how slices encompass characters from a starting index to the end .

The error occurs due to improper use of quotation marks. In Python, strings enclosed in single quotes cannot contain an unescaped single quote within them without causing a syntax error. To fix this, the string should be enclosed in double quotes or the single quote inside the string should be escaped with a backslash .

You might also like