• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
info@simplycoding.in
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Python

Python Strings

  • Categories Python, Python Strings, Python

In this post we will cover Strings in Python. You can watch our videos on Strings in Python – Click Here

Q.1 What is string in python?
Ans.

  • Strings in Python are arrays of bytes representing unicode
  • String are surrounded by either single quotation marks, or double quotation marks.
  • Python does not have a character data type, a single character is simply a string with a length of 1.
  • String are immutable
  • e.g. ‘Hello World ‘

Q.2 Give ASCII or Unicode Values for Capital, lowercase letters and numbers.
Ans:    A to Z = 65 – 92
            a to z = 97 – 122
            0 to 9 = 48 to 57

Q. 3 What are different types of string in python?
Ans.

  • Python strings can be Single line or Multiline string by either using triple quotes or using slash at the end of each line.
  • Example:
str1 = 'Hello'  # single line
print(str1)
str2 = "Hello \
           World" # Multiline
print(str2)
str3 = '''Hello
            World''' # triple quotation
print(str3)

Q. 4 What do you mean by strings are immutable in python?
Ans. Python strings are “immutable” means they cannot be changed after they are created. We can create new strings by changing existing string.

Q. 5 What is the output of using following operators in Python?
                 a = ‘Simply’
                 b = ‘Coding’
         What is output of :

    1. print (a+b)
    2. print (a*2)
    3. ‘im’ in a
    4. ‘c’ in b
    5. ‘k’ not in b
    6. S not in a

Ans. 

    1. SimplyCoding
    2. SimplySimply
    3. True
    4. False
    5. True
    6. False

Q.7 What do the following built-in functions do 1. ord(), 2. chr()? 
Ans.

1. ord(): This function returns the ASCII value of character passed. E.g.
ord(‘A’)
>> 65

2. chr(): This function converts number (int) to its equivalent ASCII value. E.g.
chr(65)
>> A

Q. 7 Which of the following function check if all characters are digit?

    1. isalnum()
    2. isdecimal()
    3. isnumeric()
    4. isdigit()

Ans. d

Q. 8 Why we use isalnum() String method?
Ans. The isalnum() method returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9).
Syntax: string.isalnum()

Q. 9 What is the use of identifier() method in String?
Ans.

  • It returns True if the string is a valid identifier, else False.
  • A string is considered a valid identifier if it only holds alphanumeric letters (a-z) and (0-9), or underscores.
  • A valid identifier cannot start with a number, or contain any spaces.

10. String Method capitalize() converts _____ character to Capital Letter/ Letters.

  1. All
  2. First
  3. Last
  4. All of above

 Ans. b

Q. 11 _____ String Method returns Length of an Object.

  1. count()
  2. len()
  3. center()
  4. None of above.

 Ans. b

Q. 12 Why we use String center() Method?
Ans.

  • We can center align the string with this function
  • using a specified character (space is default) as the fill character.
  • Syntax:  string.center(length [, character])
  • Example:
    txt = “Simply”
    x = txt.center(10, “@”)
    print(x)
  • Output: @@Simply@@

Q. 13 String function find() returns index of ______ occurrence of substring.

    1. First
    2. Last
    3. All
    4. All of above

 Ans. a

Q. 14 Write short note on replace method.
Ans.

  • As we know strings are immutable so replace method doesn’t really replace but makes a new string with the replacement made.
  • Syntax: str.replace(old, new [, count])
  • Example:
    str1 = ‘Hello, welcome’
    print(str1.replace(‘Hello’, ‘hi..’))  # replacing ‘Hello’ with ‘hi..’output:  hi.., welcome

Q.15 What is String Formatting? Give an example.
Ans.

  • The “%” operator is used to format a set of variables enclosed in a “tuple” (a fixed size list), together with a format string, which contains normal text together with “argument specifiers”, special symbols like “%s” and “%d”.
  • Formatting creates a new string (because strings are immutable)
  • Example:
    greeting = “Hello”
    print( “%s. Welcome to python.” % greeting )
  • Output:      Hello. Welcome to python.’

Q. 16 Write a program to input any string and to find the number of words in the string.
Ans.

str = "Honesty is the best policy"
words = str.split()
print (len(words))

OUTPUT: 5

Q. 17 Write a program to convert binary to decimal
Ans.

binary = input("Enter the binary string")
decimal=0
for i in range(len(str(binary))):
   power=len (str (binary)) - (i+1)
   decimal+=int(str(binary)[i])*(2**power)
print (decimal)

OUTPUT
Enter the binary string1100011
99

Q. 18 Write a Python script that takes input from the user and displays that input back in upper and lower cases.
Ans.

user_input = input("What's your favourite language? ")
print("My favourite language is ", user_input.upper())
print("My favourite language is ", user_input.lower())

Q.19 If string str = “amazing” what is the output of str[-5] + str[-1]?
Ans. ag

Q.20 What are the result of following statements –

print ("""
1
2
3
""")
print ("One","Two"*2)
print ("One" + "Two"*2)
print (len('012345678910'))
s="987654321"
print (s[-1],s[-3])
print (s[-3:],s[:-3])
print (s[-100:-3],s[-100:3])

Ans.

1
2
3
One TwoTwo
OneTwoTwo
12
1 3
321 987654
987654 987

Q. 21 What will this code print:

pattern=""
for a in range (1,6):
   pattern += str(a)
print (pattern)

Ans.

1
12
123
1234
12345

STRING METHODS: 

MethodDescription
Python String isalnum()Checks Alphanumeric Character
Python String isalpha()Checks if All Characters are Alphabets
Python String isdecimal()Checks Decimal Characters
Python String isdigit()Checks Digit Characters
Python String isidentifier()Checks for Valid Identifier
Python String islower()Checks if All Characters are Lowercase
Python String upper()Converts to uppercase Characters
Python String lower()Converts to lowercase Characters
Python String capitalize()Converts first character to Capital Letter
Python String center()Pads string with specified character
Python len()Returns Length of an Object
Python String strip()Removes leading and trailing whitespaces and returns string
Python String lstrip()Removes leading whitespaces and returns string
Python String rstrip()Removes trailing whitespaces and returns string
Python String find()Returns the index of first occurrence of substr
Python String count()returns occurrences of substring in string

  • Share:
author avatar
Simply Coding

Previous post

HTML Table Tags
June 14, 2021

Next post

Python List
June 15, 2021

You may also like

Python function
Python function
3 July, 2021
for & while
Learn Python for & while
19 June, 2021
Queue
Python Queue
17 June, 2021

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now