0% found this document useful (0 votes)
8 views37 pages

1 Strings

The document provides an overview of strings in Python, detailing their characteristics, methods, and operations such as concatenation, slicing, and formatting. It explains how to access and manipulate strings using various built-in functions and operators. Additionally, it covers string comparison and the use of the 'str.format()' method for creating formatted strings.

Uploaded by

SARIKA NAIR
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)
8 views37 pages

1 Strings

The document provides an overview of strings in Python, detailing their characteristics, methods, and operations such as concatenation, slicing, and formatting. It explains how to access and manipulate strings using various built-in functions and operators. Additionally, it covers string comparison and the use of the 'str.format()' method for creating formatted strings.

Uploaded by

SARIKA NAIR
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

Strings

What is a String?

 A string is a sequence of
characters.

 It can contain letters,


numbers, symbols, and
spaces.

 Strings are enclosed in single(‘


’ ‘ ‘),double(‘ ” “ ’), or triple (‘
‘’’ ‘’’ ‘) or (‘ “ “ “ ‘’ ‘’ ‘’ ‘)
quotes.
String Basics

String Length
 You can find the length of a
string using the ‘len()’
function
Accessing Characters

 Individual characters within a


string can be accessed using
indexing.

 Python uses zero-based


indexing, so the first
character is at index 0.
String Concatenation

Strings can be combined using


the ‘+’ operator.
String Methods

Strings have built-in methods to


perform various operations.

Changing Case:

 ‘[Link]()’: Converts a string to


uppercase.

 ‘[Link]()’: Converts a string to


lowercase.
Removing Whitespace
 ‘[Link]()’: Removes leading and trailing
whitespace.

Replacing Text
 ‘[Link](old,new)’: Replaces
occurrences of ‘old’ with ‘new’

Splitting Strings
 ‘[Link](separator)’: Splits a string into a
list using the specified ‘separator.
String Formatting

‘[Link]()’: Used to
format strings with
placeholders ‘{}’
String Slicing

 Slicing allows you to extract


substrings from a string.

 Syntax: ‘string[start:end]’ (inclusive


of ‘start’, exclusive of ‘end’).

 You can also use a step value for


striding:
Syntax: ‘string[start:end:step]’
String Comparison

 Strings can be compared


using comparison operators
(‘==‘, ‘!=’, ‘<’, ‘>’, ‘<=’,
‘>=’).

 Comparison is case-sensitive.
Indexing and Negative Indexing

You can access characters from


the end of a string using
negative indexing.
Comparing Strings in
Python

• compare strings to determine their order or equality.


• String comparisons are case-sensitive, meaning uppercase and lowercase letters are
considered different.
Comparison Operators

1. Equality(‘==’)
Checks if two strings are equal.

2. Inequality(‘!=‘)
Checks if two strings are not
equal.
3. Greater Than (‘>’) and Less
Than (‘<’)
Compares strings based on their
lexicographic (dictionary) order.

4. Greater Than or Equal To (‘>=’)


and Less Than or Equal To (‘<=’)
Checks if one string is greater or less
than or equal to another.
String Comparison Examples

 When comparing strings, Python


checks each character's ASCII value
from left to right.

 The first character that differs


determines the result.
For strings with different lengths,
the shorter string is considered less
than the longer one.
Using Comparison in Conditional
Statements

String comparisons are often used


in conditional statements (if-else)
to make decisions in your code.
Remember to account for case
differences if needed, using
‘[Link]()’ or ‘[Link]()’
Slicing and Striding
Strings
Slicing and striding are techniques used to extract specific portions of a string in
Python.
Slicing Strings
 Slicing allows you to extract a portion (substring) of a string by specifying a start and
end index.
 The result includes all characters from the start index (inclusive) up to, but not
including, the end index (exclusive).
 If you omit the start index, it defaults to 0.
 If you omit the end index, it defaults to the length of the string.

 Negative indices can also be used for slicing. ‘-1’ refers to the last character.
Striding Strings

 Striding (also called slicing with a step)


allows you to extract characters from a
string with a specified step size.
 Syntax: ‘string[start:end:step]’

 The ‘step’ value determines how many


characters to skip between each
extraction.
 A positive ‘step’ moves from left to right,
while a negative ‘step’ moves from right to
left.
Striding with Negative Indices

Combining negative indices with


striding allows for reverse
extraction.
Summary

 Slicing and striding are commonly used for parsing text and extracting
specific information from strings.
 Useful for working with data like dates, URLs, or file paths.
 Slicing extracts a substring by specifying start and end indices.
 Striding extracts characters with a specified step size.
 Use negative indices for reverse extraction.
 These techniques are essential for manipulating and analyzing text data in
Python.
String Operators and
Methods

In Python, strings are a fundamental data type, and they come with
various operators and methods for performing operations and
transformations.
Common String Operators

1. Concatenation (‘+’)
The (‘+’) operator is used to
concatenate (combine) two or more
strings.

2. Repetition (‘*’)
The (‘*’) operator allows you to
repeat a string multiple times.
Common String Methods

1. Changing Case
 ‘[Link]()’: Converts a string to
uppercase.

‘[Link]()’: Converts a string to
lowercase.

2. Removing Whitespace
 ‘[Link]()’: Removes leading and
trailing whitespace (spaces, tabs,
newlines).
3. Replacing Text
 ‘[Link](old,new)’: Replaces
occurrences of ‘old’ with ‘new’.

4. Splitting Strings
 ‘[Link](separator)’: Splits a
string into a list using the
specified ‘separator.
5. Finding Substrings
 ‘[Link](substring)’: Searches for
‘substring’ and returns its index or ‘-1’ if
not found.

6. Checking Prefix and Suffix


 ‘[Link](prefix)’: Checks if the
string starts with the specified ‘prefix’.
 ‘[Link](suffix)’: Checks if the
string starts with the specified ‘suffix’.
Summary

 Python provides several operators (‘+’, ‘*’) for working with


strings.
 String methods allow you to manipulate and transform strings
in various ways, such as changing case, removing whitespace,
replacing text, and splitting strings.
 Understanding these operators and methods is essential for
effective string manipulation in Python.
String Formatting with
‘[Link]()’

• String formatting is a technique for constructing strings by inserting


values into predefined placeholders within a string.
• Python provides the ‘[Link]()’ method for this purpose
Using ‘[Link]()’

 ‘[Link]()’ is a method that allows


you to create formatted strings with
placeholders ‘{}’.

 In this example, ‘{}’ acts as a


placeholder for values that will be
inserted.
Positional Arguments

 ‘[Link]()’ inserts values


based on their position

 The first ‘{}’ is replaced with


‘name’ , and the second ‘{}’
is replaced with ‘age’
Named Arguments

 You can also use named placeholders


to specify which value to insert into
each placeholder.

 Using named placeholders makes the


code more readable and maintainable.
Accessing Variables and
Expressions

You can insert variables and


expressions within placeholders
Formatting Options

 You can apply formatting


options to control how
values are displayed.

 In this example, ‘:.2f’ formats


the ‘pi’ value as a floating-
point number with two
decimal places.
Summary

 ‘[Link]()’ is a versatile method for creating formatted


strings in Python.
 Placeholders ‘{}’ are used to specify where values should
be inserted.
 Positional and named arguments can be used for value
insertion.
 Formatting options can be applied to control the display of
values.

You might also like