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

Strings

Uploaded by

madiha yousaf
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)
17 views

Strings

Uploaded by

madiha yousaf
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
You are on page 1/ 23

Python….

Strings
Outline

 Strings
 Control Codes within Strings
 Slicing & Indexing Strings
 Concatenation
 Arithmetic Operators with Strings.
Strings and Characters
 In addition to processing numeric values, you can process
strings in Python.
 A string is a sequence of characters and can include text and
numbers.
Str = “Hello4you”
 String values must be enclosed in matching single quotes (') or
double quotes (").
 Python does not have a data type for characters.
 A single-character string represents a character. For example,
letter = 'A' # Same as letter = "A"
numChar = '4' # Same as numChar = "4"
message = "Good morning" # Same as message = 'Good morning‘

A string is a sequence of characters. Python treats


characters and strings the same way.
3
Character encoding
 Computers use binary numbers internally.
 A character is stored in a computer as a sequence of 0s and
1s.
 Mapping a character to its binary representation is called
character encoding.
 There are different ways to encode a character.
 The manner in which characters are encoded is defined by an
encoding scheme.

4
Character encoding: ASCII code
 One popular standard of character encoding is ASCII
(American Standard Code for Information
Interchange)
 7-bit encoding scheme for representing all
uppercase and lowercase letters, digits, punctuation
marks, and control characters.
 ASCII uses numbers 0 through 127 to represent
characters.

5
Character encoding: Unicode code

 Python also supports Unicode.


 Unicode is an encoding scheme for representing
international characters.
 ASCII is a small subset of Unicode.
 Unicode was established by the Unicode Consortium to
support the interchange, processing, and display of written
texts in the world’s diverse languages.
 A Unicode starts with \u, followed by four hexadecimal digits
that run from \u0000 to \uFFFF.

6
The ord and chr Functions

 Python provides the ord(ch) function for returning the ASCII


code for the character ch and the chr(code) function for
returning the character represented by the code. For example,

>>> ch = 'a'
>>> ord(ch)
97
>>> chr(98)
'b'
>>> ord('A')
65
>>>
7
Escape Sequences for Special
Characters

 Suppose you want to print a message with quotation marks


in the output. Can you write a statement like this?
print("He said, "John's program is easy to read"")
 No, this statement has an error.
▪ Python thinks the second quotation mark is the end of the string and does
not know what to do with the rest of the characters.
▪ To overcome this problem, Python uses a special notation to represent
special characters.
 This special notation, which consists of a backslash (\)
followed by a letter or a combination of digits, is called an
escape sequence.

8
Escape Sequences for Special
Characters

9
Escape Sequences for Special
Characters

 The \n character is also known as a newline, line break or


end-of-line (EOL) character, which signifies the end of a
line.
 The \f character forces the printer to print from the next
page.
 The \r character is used to move the cursor to the first
position on the same line.

10
Printing without the Newline
 When you use the print function, it automatically
prints a linefeed (\n) to cause the output to advance
to the next line.
 If you don’t want this to happen after the print
function is finished, you can invoke the print
function by passing a special argument
 end = "anyendingstring"
 using the following syntax:
▪ print(item, end = "anyendingstring")

11
Printing without the Newline
 For example:
radius = 3
print("The area is", radius * radius * math.pi, end = ' ')
print("and the perimeter is", 2 * radius)

 Displays:
The area is 28.26 and the perimeter is 6

12
The str Function

 The str function can be used to convert a number into a


string. For example:

13
String indexing
 Each character in a string has a numbered position called an
index.
 Character at the nth position in a string can be accessed by
putting the number n between two square brackets ([])
immediately after the string

Forgetting that counting starts with zero and


trying to access the first character in a string
with the index 1 results in an off-by-one error.
Index Error and negative Indexing
 The largest index in a string is always one less than the
string’s length.
 If you try to access an index beyond the end of a string, then
Python raises an IndexError:

 Strings also support negative indices

 Just like with positive indices, Python raises an


IndexError if you try to access a negative index less
than the index of the first character in the string:
String Slicing
To extract a portion of a string is called Slicing.
Each character can be accessed through index. The first three letters
of the string "fig pie“ can be sliced.

flavor[0:3] is called a slice


If you omit the first index in a slice, then Python assumes you want to start at index
0:
Similarly, if you omit the second index in the slice, then Python assumes that it
ends with the last character in the string
If you omit both the first and second numbers in a slice, you get a string that starts
with the character at index 0 and ends with the last character
String Slicing
 Python won’t raise an IndexError when you
try to slice between boundaries that fall
outside the beginning or ending boundaries
of a string
The String Concatenation Operator

 You can use the + operator to add two numbers.


 The + operator can be used to concatenate two strings.
 For example:

18
Reading Strings from the Console

 To read a string from the console, use the input function.

s1 = input("Enter a
string: ")
s2 = input("Enter a
string: ")
s3 = input("Enter a
string: ")
print("s1 is " + s1)
print("s2 is " + s2)
print("s3 is " + s3)
19
Mathematical Functions, Strings, and
Objects
 In Python, a number is an object, a string is an
object, and every datum is an object
Arithmetic Operators with Strings
 The + operator concatenates two strings together, which is
why the result of "2" + "2" is "22" and not "4".

 Strings can be multiplied by a number as long as that


number is an integer or whole number but the result is
concatination.

 Python raises a TypeError when a string is multiplied by a a


non-integer.

 Python throws a TypeError because it expects the objects on


both sides of the + operator to be of the same type.
Practice Examples

1. Create a string and print its length using len()


2. Create two strings, concatenate them, and
print the resulting string.
3. Create two strings, use concatenation to add a
space between them, and print the result.
4. Print the string "zing" by using slice notation
to specify the correct range of characters in
the string "bazinga".
Summary

 Strings
 Control Codes within Strings
 Slicing & Indexing Strings
 Concatenation
 Arithmetic Operators with Strings.

You might also like