0% found this document useful (0 votes)
17 views17 pages

Python - Lesson6 - String (Revised)

Uploaded by

mttpkxr98s
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
17 views17 pages

Python - Lesson6 - String (Revised)

Uploaded by

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

Python – Lesson 6

String
Form 4
Python String Basics
In Python code, a string is written within double
quotes, e.g. "Hello", or alternately within single quotes
like 'Hi'. Use the len(s) function to get the length of a
string, and use square brackets to access individual
chars inside the string. The chars are numbered
starting with 0, and running up to length-1.
Python String Basics
s = 'Hello'
print(len(s)) ## 5
## Chars are numbered starting with 0
print(s[0]) ## 'H'
print(s[1]) ## 'e'
print(s[4]) ## 'o' -- last char is at length-1
print(s[5]) ## ERROR, index out of bounds
Python String Basics
For example:
Python String Basics
Python strings are "immutable" which means a string
can never be changed once created. Use + between
two strings to put them together to make a larger
string.

s = 'Hello'
t = 'Hello' + ' hi!' ## t is 'Hello hi!'
String Slices
A "slice" in Python is powerful way of referring to sub-
parts of a string. The syntax is s[i:j] meaning the
substring starting at index i, running up to but not
including index j.

index 0 1 23 4

Hello
String Slices
If the first slice number is omitted, it just uses the start
of the string, and likewise if the second slice number is
omitted, the slice runs through the end of the string.

index 0 1 23 4

Hello
String Slices
Use the slice syntax to refer to parts of a string and +
to put parts together to make bigger strings.
Negative Index
As an alternative, Python supports using negative
numbers to index into a string:
-1 means the last char, -2 is the next to last, and so on.
In other words, -1 is the same as the index len(s)-1,
-2 is the same as len(s)-2. The regular index numbers
make it convenient to refer to the chars at the start of the
string, using 0, 1, etc. The negative numbers work
analogously for the chars at the end of the string with -1,
-2, etc. working from the right side.
Negative Index
Python String Loops
One way to loop over a string is to use
the range(n) function which given a number, e.g. 5,
returns the sequence 0, 1, 2, 3 ... n-1. Those values
work perfectly to index into a string, so the loop for i in
range(len(s)) will loop the variable i through the index
values 0, 1, 2, ... len(s)-1, essentially looking at each
char once.
Python String Loops

Since we have the index number, i, each time through the


loop, it's easy to write logic that involes chars near to i, e.g.
the char to the left is at i-1.
Python String Loops
Another way to refer to every char in a string is with the
regular for loop:

This form is an easy way to look at each char in the string,


although it lacks some of the flexibility of the i/range() form
above.
Exercise 6 - Reverse output number 0 to 9

Hint:
• The chr() function can return the character corresponding to
the ASCII code.
• Use a For loop to retrieve numbers 0 to 9 first.

Submit the file


F4@##Reverse.py
to teacher.
ASCII Table
Case Changing of Strings
The below Python functions are used to change the case of the
strings. Let’s look at some Python string methods with examples:
•lower(): Converts all uppercase characters in a string into
lowercase
•upper(): Converts all lowercase characters in a string into
uppercase
•title(): Convert string to title case
•islower(): Checks if all characters in the string are lowercase
•capitalize(): Convert the first character of a string to uppercase
•count(): Returns the number of occurrences of a substring in the
string.
To convert string to upper case

You might also like