ICT Lecture 22
ICT Lecture 22
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
6
The ord and chr Functions
>>> ch = 'a'
>>> ord(ch)
97
>>> chr(98)
'b'
>>> ord('A')
65
>>>
7
Escape Sequences for Special
Characters
8
Escape Sequences for Special
Characters
9
Escape Sequences for Special
Characters
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
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
18
Reading Strings from the Console
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
Control Codes within Strings
Slicing & Indexing Strings
Concatenation
Arithmetic Operators with Strings.