0% found this document useful (0 votes)
47 views6 pages

04 Strings Casting PDF

This document provides a comprehensive guide to Python strings, explaining their basic understanding, creation methods, operations, and formatting techniques in both Roman Urdu and English. It covers important concepts such as string immutability, escape sequences, and common string methods, along with a section of multiple-choice questions for practice. The guide aims to enhance understanding and proficiency in working with strings in Python.

Uploaded by

Rehan Sabir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views6 pages

04 Strings Casting PDF

This document provides a comprehensive guide to Python strings, explaining their basic understanding, creation methods, operations, and formatting techniques in both Roman Urdu and English. It covers important concepts such as string immutability, escape sequences, and common string methods, along with a section of multiple-choice questions for practice. The guide aims to enhance understanding and proficiency in working with strings in Python.

Uploaded by

Rehan Sabir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Understanding Python Strings

A Comprehensive Guide with Roman Urdu and English Explanations

Repo(4)
Python Strings – Roman Urdu +
English Explanation
Python mein strings kaafi important concept hain. Yahan hum strings ko detail mein
samjhenge, unka istemaal, aur unke saath kiye ja sakne wale operations.

Strings Ki Basic Samajh (Basic Understanding of Strings)


Roman Urdu:
Python mein string characteron ka sequence hota hai jo quotes mein enclosed hota hai
(single, double ya triple quotes). Strings immutable hain, yani banne ke baad unko change
nahi kiya ja sakta.

English:
In Python, a string is a sequence of characters enclosed in quotes (single, double, or triple).
Strings are immutable, meaning they cannot be changed after creation.

String Banane Ke Tareeqay (Ways to Create Strings)


Roman Urdu:

Single quotes: 'Hello'


Double quotes: "Hello"
Triple quotes: '''Multi-line strings ke liye'''
Raw strings: r'Hello\nWorld' (backslashes ko as-it-is leta hai)

English:

Single quotes: 'Hello'


Double quotes: "Hello"
Triple quotes: '''For multi-line strings'''
Raw strings: r'Hello\nWorld' (treats backslashes literally)

Escape Sequence Characters


Roman Urdu:

Ye special characters hote hain jo backslash \ ke sath use hote hain:

\n - New line
\t - Tab
\\ - Backslash itself
\" - Double quote in string

English:

These are special characters used with backslash:

\n - New line
\t - Tab
\\ - Backslash itself
\" - Double quote in string

String Operations
Roman Urdu:

Concatenation (+): Do strings ko jorna


"Hello" + "World" # Output: HelloWorld
Indexing: String ke characters ko index se access karna
"Hello"[1] # Output: 'e'
Slicing: String ka hissa lena
"Hello"[1:3] # Output: 'el'
Length (len()): String ki length maloom karna
len("Hello") # Output: 5

English:

Concatenation (+): Joining two strings


Indexing: Accessing characters by index
Slicing: Getting part of a string
Length (len()): Finding string length

String Formatting
Roman Urdu:

Old style (% operator):


"Name: %s, Age: %d" % ("Ali", 25)
format() method:
"Name: {}, Age: {}".format("Ali", 25)
f-strings (Python 3.6+):
name = "Ali"
f"Name: {name}"

English:

Old style (% operator)


format() method
f-strings (most modern way)

String Pool aur Interning


Roman Urdu:
Python string literals ko memory mein optimize karne ke liye string pool use karta hai. Agar do
variables same string value rakhte hain, to possible hai ke dono same memory location ko
point karein (lekin hamesha nahi).

English:
Python uses string pooling to optimize memory usage of string literals. If two variables have
the same string value, they may point to the same memory location (but not always).

Type Casting (Type Conversion)


Roman Urdu:

Implicit: Python khud convert karega (jab safe ho)


10 + 5.5 # Output: 15.5
Explicit: Hum manually convert karein
int("10"), str(10), float("3.14")

English:

Implicit: Python automatically converts when safe


Explicit: Manual conversion using functions

Aam String Methods (Common String Methods)


Roman Urdu:

.split() → String ko list mein divide karega


.join() → List ko string mein combine karega
.replace() → Text replace karega
.find() → Text ka position batayega
.upper() / .lower() → Case change karega

English:
.split() → Splits string into list
.join() → Joins list into string
.replace() → Replaces text
.find() → Finds text position
.upper() / .lower() → Changes case

Important Notes
Roman Urdu:

Strings immutable hain – change nahi kar sakte, nayi string banegi
Comparison dictionary order se hota hai
== content check karta hai, is memory location check karta hai

English:

Strings are immutable – cannot be changed, new string is created


Comparison is lexicographical (dictionary order)
== checks content, is checks memory location

✅ Ye basic concepts hain strings ke Python mein. Inko bar bar practice karo taake achi samajh
aa jaye! If you want MCQs on this topic too, let me know — I can make a quiz as well!

Python Strings – MCQs with Answers & Explanation


1. Which of the following is a valid string in Python?
A) Hello
B) 'Hello'
C) Hello"
D) Hello'
2. What is the output of "Python"[1]?
A) 'P'
B) 'y'
C) 't'
D) 'o'
3. Which method is used to convert all characters of a string to uppercase?
A) upper()
B) uppercase()
C) cap()
D) toUpper()
4. Strings in Python are:
A) Mutable
B) Immutable
C) Changeable
D) Optional
5. What does len("Hello") return?
A) 4
B) 5
C) 6
D) Error
6. What will be the output of "Hello" + "World"?
A) Hello World
B) Hello+World
C) HelloWorld
D) Error
7. What is the result of "Hello"[1:4]?
A) 'ell'
B) 'el'
C) 'llo'
D) 'hel'
8. Which of the following is used to insert a new line in a string?
A) \t
B) \
C) \n
D) \a
9. Which operator is used for string formatting (old style)?
A) *
B) %
C) #
D) $
10. What does is keyword compare in strings?
A) Content
B) Length
C) Memory location
D) Data type

Answers and Explanation:


1. Answer: B) 'Hello'
Explanation: Python mein strings quotes ke andar likhi jaati hain. 'Hello' is valid.
Roman Urdu: Quotes ke bagair string valid nahi hoti.
2. Answer: B) 'y'
Explanation: Indexing 0 se start hoti hai. "Python"[1] means second character → 'y'.
Roman Urdu: Pehla character P hai (index 0), doosra y (index 1).
3. Answer: A) upper()
Explanation: .upper() method string ko uppercase mein convert karta hai.
Roman Urdu: Sab letters ko capital banata hai.
4. Answer: B) Immutable
Explanation: Strings cannot be changed after creation.
Roman Urdu: Ban jaane ke baad string ko directly change nahi kar sakte.
5. Answer: B) 5
Explanation: "Hello" mein 5 characters hain, so len("Hello") = 5.
Roman Urdu: Har character count hota hai including capital letters.
6. Answer: C) HelloWorld
Explanation: + se concatenation hoti hai → dono strings jurtay hain.
Roman Urdu: Beech mein koi space nahi, sirf strings milti hain.
7. Answer: A) 'ell'
Explanation: "Hello"[1:4] gives characters from index 1 to 3 (not 4).
Roman Urdu: Index 1 → e, 2 → l, 3 → l.
8. Answer: C) \n
Explanation: \n is used for new line in strings.
Roman Urdu: String ko next line mein le jaata hai.
9. Answer: B) %
Explanation: Old style formatting uses % operator.
Roman Urdu: %s, %d etc. purane tareeqe ka string format karte hain.
10. Answer: C) Memory location
Explanation: is operator memory address (identity) compare karta hai.
Roman Urdu: Agar do strings same jagah par memory mein stored hain.

✅ Let me know agar aapko aur MCQs chahiyein ya aap test file/PDF banana chahte ho!

created by : Hamza Rafique

You might also like