csaccept.com is a computer awareness website dedicated to providing reliable and easy-to-understand information about computer technology and digital safety. The website focuses on educating students, beginners, and general users about computer basics, cyber security, emerging technologies, and practical IT skills. Through informative articles, quizzes, and real-life examples, csaccept.com aims to increase digital literacy and help users stay safe and confident in today’s technology-driven world.
Facebook ………………..
Instagram ……………..
Twitter ( X ) ……………..
YouTube
Strings in Python with suitable example
Introduction to Strings
In Python, strings are a fundamental data type used to represent collections of characters. They are commonly used to store and manipulate text-based data.
Strings can be defined by enclosing characters or sequences of characters within:
- Single quotes (
' ') - Double quotes (
" ") - Triple quotes (
''' '''or""" """)
Internally, the computer stores and processes characters as combinations of 0s and 1s using ASCII or Unicode encoding. Therefore, strings in Python are also referred to as collections of Unicode characters.
Using strings, we can:
- Handle text-based information
- Manipulate and process textual data
- Perform string-specific operations in Python programming
Syntax
str = "Hi there!"
Creating Strings in Python
In Python, strings can be created by enclosing characters within single quotes, double quotes, or triple quotes.
Example
str1 = 'Hello' # Single quotes
str2 = "World" # Double quotes
str3 = """Python is powerful language.""" # Triple quotes
print(str1)
print(str2)
print(str3)
Output
Hello
World
Python is powerful language.
Explanation:
In the above example, three different strings are created using single quotes, double quotes, and triple quotes. All three methods are valid for creating strings in Python.
String Indexing
In Python, strings are indexed starting from 0, just like in many other programming languages.
Example
str = "HELLO"
| Character | H | E | L | L | O |
|---|---|---|---|---|---|
| Index | 0 | 1 | 2 | 3 | 4 |
str[0] = "H"
str[1] = "E"
str[2] = "L"
str[3] = "L"
str[4] = "O"
Explanation:
Each character in the string "HELLO" is assigned an index starting from 0.
String indexing is important because it allows us to access and manipulate specific characters within a string.
Deleting a String
In Python, a string can be deleted in two ways:
- Assigning the value
None - Using the
delkeyword
1. Assigning None to a String
my_string = "Hello, World"
my_string = None
Explanation:
The variable my_string is assigned the value None, which effectively removes the string value.
2. Using the del Keyword
my_string = "Hello, World"
del my_string
Explanation:
The del keyword deletes the string object completely. After execution, the variable no longer exists, and the memory occupied by the string is freed.
String Operations in Python
Python provides several operators to perform operations on strings.
1. Concatenation (+)
The concatenation operator is used to combine two or more strings into a single string.
Example
str1 = "Hello"
str2 = "World"
result = str1 + str2
print(result)
Output
HelloWorld
2. Repetition (*)
The repetition operator is used to repeat a string multiple times.
Example
str1 = "Hello"
result = str1 * 3
print(result)
Output
Hello Hello Hello
3. Indexing ([ ])
The indexing operator allows access to individual characters of a string by their position.
Example
str1 = "Hello"
print(str1[0]) # H
print(str1[3]) # l
4. Slicing ([ : ])
The slicing operator extracts a substring by specifying a range of indices.
Example
str1 = "Hello World"
print(str1[6:11])
Output
World
5. Membership Operators (in, not in)
Membership operators check whether a substring exists in a string.
Example
str1 = "Hello World"
print("World" in str1) # True
print("python" not in str1) # True
print("print" in str1) # False
print("Hello" in str1) # True
Short Questions & Answers (Exam Friendly):
1. What is a string in Python?
A string is a collection of characters used to store and manipulate text in Python.
2. Which data type is used to store text in Python?
String data type.
3. How are strings represented internally in a computer?
Strings are stored using ASCII or Unicode encoding as combinations of 0s and 1s.
4. Which quotes are used to create strings in Python?
Single quotes, double quotes, and triple quotes.
5. What is Unicode?
Unicode is a character encoding standard that represents characters from all languages.
6. Are Python strings mutable or immutable?
Strings are immutable.
7. What is string indexing?
String indexing is accessing characters of a string using index numbers.
8. From which index does Python string indexing start?
Indexing starts from 0.
9. What is slicing in Python?
Slicing is extracting a part of a string using a range of indices.
10. What does str[0] represent?
It represents the first character of the string.
11. What is string concatenation?
Combining two or more strings using the + operator.
12. Which operator is used for string repetition?
The * operator.
13. What does "Hello" * 2 return?
HelloHello
14. What is the use of in operator?
To check if a substring exists in a string.
15. What is the use of not in operator?
To check if a substring does not exist in a string.
16. How can a string be deleted in Python?
By assigning None or using the del keyword.
17. What does the del keyword do?
Deletes the variable and frees memory.
18. Can we change a character of a string directly?
No, strings are immutable.
19. What is triple-quoted string used for?
For multi-line strings or large text.
20. Write the syntax to create a string.
str = "Hello"
MCQ – Multiple Choice Questions:
1. What is a string in Python?
a) Number
b) Collection of characters
c) Boolean
d) Function
✅ Answer: b
2. Which symbol is used for string concatenation?
a) *
b) –
c) +
d) /
✅ Answer: c
3. Python strings are stored using which encoding?
a) Binary
b) ASCII
c) Unicode
d) Both b and c
✅ Answer: d
4. Which quote is NOT valid for strings?
a) ‘ ‘
b) ” “
c) ”’ ”’
d) { }
✅ Answer: d
5. Indexing in Python starts from?
a) 1
b) -1
c) 0
d) 2
✅ Answer: c
6. What is the index of “H” in “HELLO”?
a) 1
b) 0
c) 2
d) -1
✅ Answer: b
7. What does str[2] return in "HELLO"?
a) H
b) E
c) L
d) O
✅ Answer: c
8. What is slicing?
a) Joining strings
b) Deleting strings
c) Extracting substring
d) Repeating string
✅ Answer: c
9. What is the output of "Hi" * 3?
a) Hi
b) HiHi
c) HiHiHi
d) Error
✅ Answer: c
10. Which operator checks membership?
a) +
b) *
c) in
d) =
✅ Answer: c
11. Which keyword deletes a string variable?
a) remove
b) delete
c) del
d) clear
✅ Answer: c
12. What is the output of "World" in "Hello World"?
a) False
b) Error
c) True
d) None
✅ Answer: c
13. Which of the following is immutable?
a) List
b) Dictionary
c) String
d) Set
✅ Answer: c
14. What does None represent?
a) Zero
b) Empty string
c) No value
d) Error
✅ Answer: c
15. Which function prints output?
a) write()
b) output()
c) display()
d) print()
✅ Answer: d
16. Which slicing extracts “World” from "Hello World"?
a) [5:10]
b) [6:11]
c) [0:5]
d) [1:6]
✅ Answer: b
17. What is "A" + "B"?
a) AB
b) A B
c) Error
d) None
✅ Answer: a
18. What does "python" not in "Hello World" return?
a) False
b) Error
c) None
d) True
✅ Answer: d
19. Which operator repeats strings?
a) +
b) *
c) %
d) /
✅ Answer: b
20. Which statement is correct?
a) Strings are mutable
b) Strings are immutable
c) Strings are numeric
d) Strings are boolean
✅ Answer: b

