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 :
- print (a+b)
- print (a*2)
- ‘im’ in a
- ‘c’ in b
- ‘k’ not in b
- S not in a
Ans.
- SimplyCoding
- SimplySimply
- True
- False
- True
- 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?
- isalnum()
- isdecimal()
- isnumeric()
- 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.
- All
- First
- Last
- All of above
Ans. b
Q. 11 _____ String Method returns Length of an Object.
- count()
- len()
- center()
- 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.
- First
- Last
- All
- 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:
Method | Description |
---|---|
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 |