Podcast
Questions and Answers
What will be the output of the expression 'a' + 'bc'?
What will be the output of the expression 'a' + 'bc'?
What does the expression 'abcd'[2:] evaluate to?
What does the expression 'abcd'[2:] evaluate to?
Which method is used to combine strings in Python without using the '+' operator?
Which method is used to combine strings in Python without using the '+' operator?
What will be the output of the following Python code? str1 = 'hello'; str1[-1:]
What will be the output of the following Python code? str1 = 'hello'; str1[-1:]
Signup and view all the answers
Which arithmetic operator cannot be used with strings in Python?
Which arithmetic operator cannot be used with strings in Python?
Signup and view all the answers
What will be the output of the following Python code? example = "hello"; example.count('l')
What will be the output of the following Python code? example = "hello"; example.count('l')
Signup and view all the answers
What does the find method return when used on the string "helle" with the argument "e"?
What does the find method return when used on the string "helle" with the argument "e"?
Signup and view all the answers
How can you concatenate two strings s1 and s2 in Python?
How can you concatenate two strings s1 and s2 in Python?
Signup and view all the answers
What will be the output of the following Python code? example = "helloworld"; example[::-1].startswith("d")
What will be the output of the following Python code? example = "helloworld"; example[::-1].startswith("d")
Signup and view all the answers
What will be the output of the following Python statement? example = "snow world"; print("%s" % example[4:7])
What will be the output of the following Python statement? example = "snow world"; print("%s" % example[4:7])
Signup and view all the answers
Study Notes
Python String Manipulation and Operations
- Using
print(r"\nhello")
will output\nhello
, preserving the escape character as a string due to the raw string literal. - The statement
print('new' 'line')
outputsnewline
, as Python concatenates adjacent string literals. - The expression
print('x\97\x98')
results inx\97
, interpreting the escape sequences. - Reversing a string with
str1[::-1]
transformshelloworld
todlrowolleh
. - Hexadecimal addition with
print(0xA + 0xB + 0xC)
yields33
. - Class instantiation retains the value as a string when using
str(id)
, producing12
. - The substring
example[4:7]
from"snow world"
results inwo
.
String Methods and Properties
- The method
example.count('l')
for the string "hello" counts and returns2
. -
example.find("e")
onhelle
returns1
, indicating the first occurrence of 'e'. -
example.rfind("e")
gives4
, marking the last position of 'e' withinhelle
. - The slice and
startswith
method checks if reversed string ends with 'd', returningTrue
. - The string concatenation is valid using
s3 = s1 + s2
ands3 = s1.__add__(s2)
.
Bitwise Operations and Logical Expressions
- Bitwise OR operation on binary values
1010
and1100
results in1110
. - Right shift operation
4 >> 1
translates to2
, effectively halving the value. -
True and False
evaluates toFalse
. - The operator
<<
is used for the left shift, while>>
is for right shift.
Various Output and Error Handling
- The expression
3 ^ 3
evaluates to0
, as it represents the bitwise XOR where both bits are identical. - The operation
10 & 5
outputs0
, performed using bitwise AND. - The expression
~True
producesError
, as bitwise NOT isn't defined for boolean types.
Character and String Format Manipulation
- Usage of
chr(ord('A'))
returnsA
by converting the ASCII value back to character. - The format string method can produce structured outputs like aligning text or numbers.
- Using
print(format("Welcome", "10s"), end = '#')
formats the string within specified width.
Class and Object Methods
- Overriding the
__str__(self)
method allows for custom string representations of an object usingstr(obj)
orprint(obj)
. - To find the class type,
type(s)
returnsstr
for the strings = "hello"
. - The use of commands such as
len(s)
to find string length is standard practice.
These notes encapsulate the foundational aspects of Python string handling, bitwise operations, and class methods which are crucial for effective programming.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Test your knowledge of Python outputs with this quiz! Examine various Python statements and determine their resulting outputs. This quiz will cover string manipulation and escape sequences in Python.