0% found this document useful (0 votes)
19 views31 pages

Strings in Python

It's useful for class 11th CS students.

Uploaded by

Harshita
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
19 views31 pages

Strings in Python

It's useful for class 11th CS students.

Uploaded by

Harshita
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 31

STRINGS IN PYTHON

BY-CHARLOTTE TULLY
INTRODUCTION:
• String is a collection of alphabets , words or other characters
• Python has a built-in string class named str.
• We use single quotes or double quotes to represent a string
in python.
• Strings literal are written by enclosing a sequence of
characters in single (‘Hello python world ‘) ,, double
quotes(“Hello python world”) or in triple quotes (’’’Hello
python world’’’).
WHAT ARE STRINGS?
In python , a consecutive sequence of characters , which
are enclosed or surrounded by single (‘ ‘), or double (“ ”)
quotes , is known as a string . This sequence of
UNICODE characters may include a letter , a number ,
special character , whitespace or a backlash.
Triple quotes are typically used for strings that span
multiple lines
For example: Fruits=‘Mango Apple Grapes’
CREATING STRINGS :
Strings are among the most popular data types in python .
We can create them by simply enclosing them by simply
enclosing characters in quotes (single , double , triple ).
Python creates single quotes same as the double quotes .
Sometimes , triple quotes can be used in python , but they
are generally used to represent multiline strings and doc
string . An empty string is a string that has 0 characters .
For example::
Str1=“Hello world”
Str2=“python programming”
EMPTY STRINGS :

An empty string is a string that contains no characters. Empty


strings can be created in Python using the str() function or
assigning a variable with single or double empty quotes.
Methods like the len() function or the eq operator can identify
when a string is empty or not empty.
MULTILINE STRINGS:

• To create a multiline string in Python, you can use triple


quotes (”' or “””). This allows you to write strings that span
multiple lines, making your code cleaner and easier to read.
Here's a simple example: text = '''This is a multiline string'''
print(text) # Output: # This is a # multiline # string.
ACCESSING CHARACTERS
(INDEXING )IN A STRING
String indexing in Python is zero-based: the first character in
the string has index 0 , the next has index 1 , and so on. The
index of the last character will be the length of the string
minus one.

For example:
Str=“hello world”
TRANSVERSING A STRING

Traversing a string. Traversing just means to process every character in a


string, usually from left end to right end.

a)Iterating through string for using loop


iterate range(0, len(str)) using for loop, for every iteration to get the index
and corresponding character of the string
ITERATING THROUGH STRING USING
A WHILE
while loopLOOP
is a control flow statement which
repeatedly executes a block of code until the
condition is satisfied ..
SPECIAL STRING OPERATORS

String can be manipulated using operators like


concatenate (+) , repetition(*)and membership
operator like in and not in .
CONCATENATING STRINGS
String concatenation is a common operation in programming.
It involves joining two or more strings to create a single new
string.
REPLICATING STRINGS

The operator creates a new string by repeating multiple


copies of the same string .
MEMBERSHIP OPERATORS
Python offers two membership operators for checking whether
a particular character exists the given string or not. These
operators are ‘in’ ‘and not in’.
‘In’operator : it returns true if a character /substring exist in
the given string.
‘not in’ operator: t returns true if a character /substring does
not exist in the given string.
COMPARISON OPERATORS
Comparison operators in Python, also called relational
operators, are used to compare two operands. The six
comparison operators are 1) == or equal to, 2) != or not equal
to, 3) > or greater than, 4) >= or greater than or equal to, 5)
< or less than, and 6) <= or less than or equal to. They can be
used to compare different values in Python, such as integers or
strings.
STRING SLICING

Python slicing is about obtaining a sub-string from the given string by


slicing it respectively from start to end.
• Python slicing can be done in two ways:
• Using a slice() method
• Using the array slicing [:: ] method
EXAMPLE 1
STRINGS ARE IMMUTABLE

Strings are immutable means that the content of string cannot


be changed after it is created.
>>>>str1=‘student’
>>>>str[3]= ‘p’
Typeerror:’str1’object does not support item
assignment.
Python does not allow the programmer to change a
character in a string .
STRING METHODS AND BUILT-IN
FUNCTIONS

Python provides several built-in functions associated with the


strings . These functions allow us to easily modify and
manipulate strings . We can think of functions as being actions
that we perform on elements af astring in our code .
OTHER FUNCTIONS

In interal storage or memory of the computer , the characters


are stored in integer value. A specific value is used for a given
character and it is based ON ASCII code . There are different
numbers assigned to capital letters and small letters .
CONCLUSION :

• In this course, you learned about working with strings, which are objects that contain
sequences of character data. Processing character data is integral to programming. It is a
rare application that doesn’t need to manipulate strings to at least some extent.
• Python provides a rich set of operators, functions, and methods for working with strings.
You now know how to:
• Use operators with strings
• Access and extract portions of strings
• Use built-in Python functions with characters and strings
• Use methods to manipulate and modify string data

You might also like