Python Output Quiz
10 Questions
0 Views

Python Output Quiz

Created by
@SharpestCanto9794

Questions and Answers

What will be the output of the expression 'a' + 'bc'?

  • a
  • abc (correct)
  • bc
  • bca
  • What does the expression 'abcd'[2:] evaluate to?

  • cd (correct)
  • dc
  • ab
  • a
  • Which method is used to combine strings in Python without using the '+' operator?

  • string.concat()
  • string.merge()
  • string.join() (correct)
  • string.combine()
  • What will be the output of the following Python code? str1 = 'hello'; str1[-1:]

    <p>o</p> Signup and view all the answers

    Which arithmetic operator cannot be used with strings in Python?

    <ul> <li></li> </ul> Signup and view all the answers

    What will be the output of the following Python code? example = "hello"; example.count('l')

    <p>2</p> Signup and view all the answers

    What does the find method return when used on the string "helle" with the argument "e"?

    <p>-1</p> Signup and view all the answers

    How can you concatenate two strings s1 and s2 in Python?

    <p>s3 = s1 + s2</p> Signup and view all the answers

    What will be the output of the following Python code? example = "helloworld"; example[::-1].startswith("d")

    <p>True</p> Signup and view all the answers

    What will be the output of the following Python statement? example = "snow world"; print("%s" % example[4:7])

    <p>wo</p> 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') outputs newline, as Python concatenates adjacent string literals.
    • The expression print('x\97\x98') results in x\97, interpreting the escape sequences.
    • Reversing a string with str1[::-1] transforms helloworld to dlrowolleh.
    • Hexadecimal addition with print(0xA + 0xB + 0xC) yields 33.
    • Class instantiation retains the value as a string when using str(id), producing 12.
    • The substring example[4:7] from "snow world" results in wo.

    String Methods and Properties

    • The method example.count('l') for the string "hello" counts and returns 2.
    • example.find("e") on helle returns 1, indicating the first occurrence of 'e'.
    • example.rfind("e") gives 4, marking the last position of 'e' within helle.
    • The slice and startswith method checks if reversed string ends with 'd', returning True.
    • The string concatenation is valid using s3 = s1 + s2 and s3 = s1.__add__(s2).

    Bitwise Operations and Logical Expressions

    • Bitwise OR operation on binary values 1010 and 1100 results in 1110.
    • Right shift operation 4 >> 1 translates to 2, effectively halving the value.
    • True and False evaluates to False.
    • The operator << is used for the left shift, while >> is for right shift.

    Various Output and Error Handling

    • The expression 3 ^ 3 evaluates to 0, as it represents the bitwise XOR where both bits are identical.
    • The operation 10 & 5 outputs 0, performed using bitwise AND.
    • The expression ~True produces Error, as bitwise NOT isn't defined for boolean types.

    Character and String Format Manipulation

    • Usage of chr(ord('A')) returns A 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 using str(obj) or print(obj).
    • To find the class type, type(s) returns str for the string s = "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.

    Quiz Team

    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.

    More Quizzes Like This

    Use Quizgecko on...
    Browser
    Browser