0% found this document useful (0 votes)
13 views

Python 4th Module Strings

Uploaded by

wtcricci
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Python 4th Module Strings

Uploaded by

wtcricci
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

MODULE 4

STRINGS ,LISTS &


TUPLES IN PYTHON
1. STRINGS IN PYTHON
•Definition: In Python, strings are arrays of Unicode characters.
•No Character Data Type: Python doesn’t have a separate character data type; a single
character is simply a string with length 1.
•String Literals:
•Strings can be enclosed in single (') or double (") quotation marks.
•Examples:
•'hello' and "hello" are the same.

Assigning Strings to Variables:


•Use an equal sign (=) to assign a string to a variable.
Multiline Strings:
•Enclose multiline strings with triple quotes, either double (""") or single (''').
Example with Triple Double Quotes: Example with Triple Single Quotes

Output for Both:


1.1 Accessing Strings
•Accessing Individual Characters:
•You can access individual characters in a string by their index using square brackets [].
•Indexing starts from 0 for the first character.
•Example:

• String Slicing:
•Use slicing to access a range of characters by specifying a start index and an end index (the end
index is not included).
•Syntax: string[start:end]
Example:

Indexing Rules:
•Index Out of Range: Accessing an index beyond the string length raises an IndexError.
•Invalid Index Type: Using non-integer indices (like floats) will raise a TypeError.
Negative Indexing:
•Python allows negative indexing for strings.
•-1 refers to the last character, -2 to the second last, and so on.
•Example:

1.2. Deleting/Updating a String in Python


Strings are Immutable:
•In Python, strings are immutable, meaning once a string is created, it cannot be modified.
Example of Immutability:
•Trying to change a character in a string will cause a TypeError.

Error Message:
TypeError: 'str' object does not support item assignment
Reassigning Strings:
•Instead of modifying, you can reassign the variable to a new string.
•Example:

Deleting a String:
•While you cannot delete individual characters, you can delete the entire string variable using the del
keyword.
•Example:

Error Message:
NameError: name 'txt' is not defined
Using del Keyword:
•The del keyword can delete any object in Python, such as variables, lists, or parts of lists.
1.3. Escape Characters in Python
Purpose of Escape Characters:
•Escape characters allow inserting special or illegal characters within strings. These characters have
specific meanings in Python, like quotes within quotes.
Using the Backslash \ as an Escape Character:
•The escape character in Python is a backslash \, followed by the character you want to insert.
Example of an Illegal Character:
•A double quote within a double-quoted string will cause an error

• To fix this, use an escape sequence:


Examples of Escape Characters in Action:

1. Using Triple Quotes


Triple quotes (''' or """) let you include both single and double quotes in a string without escaping.
Example:

Why it works: Triple quotes prevent Python from confusing the inner single (') and double quotes ("),
so no escape character is needed.
2. Escaping Single Quote (\')
When using single quotes around a string, any additional single quote in the text (like in “I’m”) will end the
string if it’s not escaped.
Example:

Why it works: The \' escape sequence lets Python know that the single quote in “I’m” is part of the
text, not the end of the string.
1.4. String Special Operators:
Assume string variable a holds ‘hello’ and variable b holds ‘python’ then:
Raw String:
If you write a path like "C:\Users\Asna", Python sees
\U as a special character, which can cause errors.
But if you use a raw string like r"C:\Users\Asna",
Python treats \ as normal text, so the path stays
exactly as you typed it.
Example:
1.5. String Formatting Operator:
In Python, the formatting operator % is used to insert values into strings. It works like a placeholder
system, where % is followed by a specific letter to represent different types of values, such as strings,
integers, or floating-point numbers.
Basic Syntax:

•The type is a letter that tells Python the type of value (e.g., %s for strings, %d for integers).
•Value is what you want to insert into the placeholder.

Common Formatting Types:


•%s: String placeholder
•%d: Integer placeholder
•%f: Floating-point (decimal) number placeholder
Examples
1.String Insertion

2. Integer Insertion

3. Floating-Point Insertion

4. Combining Placeholders

You might also like