Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Strings in Python -

Uploaded by

sharath.s
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)
4 views

Strings in Python -

Uploaded by

sharath.s
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
You are on page 1/ 17

Yogesh Tyagi

@ytyagi782

All About
Strings in
Python
Ma st er T ext
la tio n in P ython
Manipu
Yogesh Tyagi
@ytyagi782

String Handling in Python

Learn how to manipulate and work


with strings in Python effectively, from
basic operations to advanced
techniques.
Yogesh Tyagi
@ytyagi782

What is a String in Python?

A string in Python is a sequence of


characters enclosed within single, double, or
triple quotes.

Examples:

Strings are immutable, meaning


Use once created, their content cannot
Cases: be changed.
Yogesh Tyagi
@ytyagi782

String Creation

Basic String
Declaration:

Use triple quotes for strings


spanning multiple lines.
Multiline
Strings

Use a backslash (\) for


Escape special characters.
Characters
Yogesh Tyagi
@ytyagi782

String Indexing

Strings in Python are indexed, with the first


character at index 0.
Positive Indexing: Left to right, starting at 0.
Negative Indexing: Right to left, starting at -1.

Examples:
Yogesh Tyagi
@ytyagi782

String Slicing

String slicing allows extracting a portion of a


string using [start:end:step].

Syntax:

Examples:
Yogesh Tyagi
@ytyagi782

String Length

The len() function returns the number of


characters in a string.

Examples:

Includes spaces and special


Key characters.
Notes: Useful for validating user input or
processing strings dynamically.
Yogesh Tyagi
@ytyagi782

Common String Methods

Python offers many built-in string


methods for manipulation.
Yogesh Tyagi
@ytyagi782

Checking Substrings

Use in and not in to check for the


presence or absence of substrings.

Examples:

These are useful for conditional


statements and filtering data.
Yogesh Tyagi
@ytyagi782

String Formatting

Python provides several ways to format strings:

Using f-strings :

Output

Using format() Method

Using % Operator:
Yogesh Tyagi
@ytyagi782

Iterating Through Strings

Strings are iterable, meaning you


can loop through each character.

Examples:

Output

This is useful for character-level processing or


transformations.
Yogesh Tyagi
@ytyagi782

String Concatenation

Strings can be concatenated using the +


operator or repeated with the * operator.

Examples:
Yogesh Tyagi
@ytyagi782

String Comparison

Strings can be compared using comparison


operators like ==, !=, <, >.

Examples:

Comparisons are case-


sensitive.
Key Notes: Use .lower() or .upper() for
case-insensitive
comparisons.
Yogesh Tyagi
@ytyagi782

String Encoding and Decoding

Convert strings to bytes using .encode() and


bytes to strings using .decode().

Examples:

This is useful for handling text in different


formats or file encodings.
Yogesh Tyagi
@ytyagi782

Common Errors
with Strings

Strings cann ot be modified


in place.
Immutable
Nature:

Accessing out-of-range
indices.
IndexError:

Mixing strings with non-strings


without conversion.

TypeError:
Yogesh Tyagi
@ytyagi782

Wrap-Up

"Master String Handling in Python for


Powerful Text Processing"
Strings are a fundamental data type in Python,
and mastering their handling enables you to
work efficiently with text-based data. From
slicing to formatting and advanced
manipulations, practice these techniques to
enhance your programming skills.
Happy coding!
Yogesh Tyagi
@ytyagi782

Follow for More

You might also like