You are on page 1of 18

Lecture 16

CSC 101 – Introduction to


Computing
Outline

 Strings, Control Codes within Strings, Slicing


& Indexing Strings, Concatenation, and
Arithmetic Operators with Strings.

 While Loop, Conditionals within a Loop,


Counting using while Loop, and break &
continue Statements.
Arithmetic Operators
Strings and Characters

 A string is a sequence of characters.


 Python treats characters and strings the same
way.
 String values must be enclosed in matching
single quotes (') or double quotes (").
 Python does not have a data type for
characters.
Character Encoding

 Mapping a character to its binary


representation is called character encoding.
 ASCII a 7-bit encoding scheme, uses numbers
0 through 127 to represent characters
 Unicode is an encoding scheme for
representing international characters.
 starts with \u, followed by four hexadecimal
digits that run from \u0000 to \uFFFF
Escape Sequence
Printing Without NewLine
Str function

 The str function can be used to convert a


number into a string.
The String Concatenation Operator

 You can use the + operator to add two


numbers. The + operator can be used to
concatenate two strings.
 The augmented assignment += operator can
also be used for string concatenation.
Reading Strings from the Console

 To read a string from the console, use the


input function.
Mathematical Functions, Strings, and
Objects
 In Python, a number is an object, a string is an
object, and every datum is an object
Formatting Number and String

format(item, format-specifier)
The While Loop
 Make sure that the loop-continuation-condition eventually
becomes false so that the loop will terminate.
 If your program takes an unusual long time to run and does
not stop, it may have an infinite loop.
 Programmers often mistakenly execute a loop one time
more or less than intended. This kind of mistake is commonly
known as the off-by-one error.
Controlling a Loop with a Sentinel
Value
 Another common technique for controlling a loop is
to designate a special input value, known as a
sentinel value, which signifies the end of the input.
 A loop that uses a sentinel value in this way is called a
sentinel-controlled loop.
 Don’t use floating-point values for equality checking
in a loop control.
The Break statement
 Two keywords, break and continue, can be used in loop
statements to provide additional controls. Using break and
continue can simplify programming in some cases.
Overusing or improperly using them, however, can make
programs difficult to read and debug
Summary

 Strings, Control Codes within Strings, Slicing


& Indexing Strings, Concatenation, and
Arithmetic Operators with Strings.

 While Loop, Conditionals within a Loop,


Counting using while Loop, and break &
continue Statements.

You might also like