0% found this document useful (0 votes)
5 views19 pages

Python Strings

This document provides a detailed study of strings in Python, covering their definition, creation, basic operations, and various methods. It highlights string immutability, formatting options, and performance considerations, along with examples for each concept. Additionally, it discusses advanced f-strings and the use of Unicode and encoding.

Uploaded by

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

Python Strings

This document provides a detailed study of strings in Python, covering their definition, creation, basic operations, and various methods. It highlights string immutability, formatting options, and performance considerations, along with examples for each concept. Additionally, it discusses advanced f-strings and the use of Unicode and encoding.

Uploaded by

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

Python Data Structures

Module 2 – Detailed Study of Strings


 Introduction to Strings
A String is a built-in data structure in Python used to
store textual data.
It is a sequence of characters enclosed in single quotes ('
'), double quotes (" "), or triple quotes (''' ''' or """ """).

 Example:
 name = "Saniya"
message = 'Welcome'
paragraph = """Python is powerful"""
print(name)
 Output:
 Saniya
Creating and Storing
Strings
A string is a sequence of characters enclosed in
quotes.

 Example:
 name = "Saniya"
 message = 'Welcome'

 Output:
 Saniya
 Welcome
Basic String Operations
 Operations include
 concatenation (+)
 repetition (*)
 len().

 Example:
a = "Hello"
b = "World"
print(a + " " + b) # Concatenation
print(a * 3) # Repetition
print(len(a)) # Length

 Output:
 Hello World
 HelloHelloHello
 5
Accessing Characters by
Index
 Index starts from 0. Negative index starts from -1.

 Example:
 text = "Python"
 print(text[0])
 print(text[-1])

 Output:
 P
 n
String Slicing and Joining
 Slicing extracts part of string. join() combines list elements.

 Example:
 word = "Programming"
 print(word[0:6])
 items = ["Python","Java"]
 print(", ".join(items))

 Output:
 Progra
 Python, Java
String Methods
 Built-in methods like
 upper()
 lower()
 strip() - strip() only removes spaces from beginning and end

 Example:
 text = " python"
 print([Link]())
 print([Link]())

 Output:
 PYTHON
 python
Formatting Strings
 Insert values using format() or f-strings.

 Example:
 name="Saniya"
 age=21
 print(f"Name: {name}, Age: {age}")

 Output:
 Name: Saniya, Age: 21
String Immutability
 Strings cannot be changed after creation.

 Example:
 text="Python"
 new_text="J"+text[1:]
 print(new_text)

 Output:
 Jython
Escape Sequences
 Special characters using backslash (\n, \t).

 Example:
 print("Hello\nWorld")

 Output:
 Hello
World
Raw Strings
 Raw strings ignore escape sequences.

 Example:
 path = r"C:\Users\Saniya"
 print(path)

 Output:
 C:\Users\Saniya
String Constants
 Predefined sets from string module.

 Example:
 import string
 print([Link])

 Output:
 0123456789
Validation Methods
 isalpha(), isdigit(), isalnum().

 Example:
 print("Python".isalpha())
 print("123".isdigit())

 Output:
 True
 True
Search and Replace
 find(), replace().

 Example:
 text="Python Programming"
 print([Link]("Pro"))
 print([Link]("Python","Java"))

 Output:
 7
 Java Programming
Split and Partition
 SPLIT()- Returns list and PARTITION()-Returns
tuple
 Example String
 text = "apple-banana-grape“

 1. split() Example
 print([Link]("-"))
 Output:
 ['apple', 'banana', 'grape’]

 2. partition() Example
 print([Link]("-"))
 Output:
 ('apple', '-', 'banana-grape')
String Translation
 maketrans() and translate().

 Example:
 text="hello"
 table=[Link]("h","j")
 print([Link](table))

 Output:
 jello
Unicode and Encoding
 encode() converts to bytes, decode() to string.

 Example:
 text="Python"
 encoded=[Link]("utf-8")
 print(encoded)

 Output:
 b'Python'
String Performance
 Use join() instead of multiple + operations.

 Example:
 words=["Python","is","powerful"]
 print(" ".join(words))

 Output:
 Python is powerful
Advanced f-Strings
 Supports expressions and formatting.

 Example:
 a=10
 b=5
 print(f"Sum={a+b}")
 print(f"{a=}")

 Output:
 Sum=15
 a=10
Summary
• Strings are immutable
• Support indexing & slicing
• Many built-in methods
• Unicode supported
• f-strings are modern formatting

You might also like