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

ICT7Slides

This document covers string manipulation and methods in Python, focusing on string indexing, slicing, concatenation, and library functions. It outlines how to declare strings, access individual characters using indices, and utilize built-in functions for string operations. The learning objectives include using string indexing and manipulation functions effectively.

Uploaded by

M K Khaing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

ICT7Slides

This document covers string manipulation and methods in Python, focusing on string indexing, slicing, concatenation, and library functions. It outlines how to declare strings, access individual characters using indices, and utilize built-in functions for string operations. The learning objectives include using string indexing and manipulation functions effectively.

Uploaded by

M K Khaing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Y10-02-CT7:

String manipulation, string


methods
Y10-02-CT7: String manipulation, string methods

Learning objectives
In this lesson you will learn to:

• use string indexing


• use string manipulation functions.

For more information on this topic, and additional student activities,


see Topic 1.3 of the student book.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT7: String manipulation, string methods

Strings
Strings are sequences of characters.
They can contain any standard keyboard character (letters, numbers,
and punctuation marks).
In Python, you declare strings by placing “ “ or ‘ ‘ around the text.
You can work with strings in a number of ways, including printing
them to the screen, as in this example.

aStringExample = "Computer Science"


print(aStringExample)

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT7: String manipulation, string methods

String indexing
As strings are sequences of characters, you can treat each of the
characters separately using the index of the character.
Indices are the position of the character within the string, starting from 0.
Let’s use the example below to look at how this works.

Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Characte C o m p u t e r S c i e n c e
r

Notice that the space counts as a character. Don’t


miss them out as it will misalign your indices.
You can use the indices to access individual characters and groups of
characters.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT7: String manipulation, string methods

String indexing

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT7: String manipulation, string methods

String slices

You’ve seen how to access individual and small groups of


characters from a string.
You can use the [ ] to access selections of characters and even
change the direction of access.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT7: String manipulation, string methods

Concatenation
You can also join strings together using the + operator.

firstString = "str"
secondString = "ing"
finalString = firstString + secondString
print(firstString)
print(secondString)
print(finalString)

str
ing
string

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT7: String manipulation, string methods

Library functions

You can manipulate strings extensively using library functions.


These are small programs written by others and compiled into
libraries.
Programmers use library functions to quickly add functionality to their
programs, without having to write, rewrite, or debug the code
themselves.
In industry, developers use libraries for a huge variety of
applications. They often create their own libraries for others to use.
There are a number of Python library functions that you can use to
work with strings.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT7: String manipulation, string methods

Python string library functions


There are lots of functions you can use that are built into Python. You
can find out more about these using the Python Documentation
online.
We will look a few of the most common and useful ones.
Function What does it do?
len(string) Returns the number of characters in the string.
<str>.isalpha() Checks that every character in the string is a letter or a
number.
<str>.lower() Makes all the letters in the string lower case.
<str>.upper() Makes all the letters in the string upper case.
<str>.rjust(widt Moves the text to the right in a new string of a given
h) width. The default fill character is a space.
<str>.center(wid Centres text in a string of a given width. The default fill
th) character is a space. Notice the American spelling here.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT7: String manipulation, string methods

In action

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT7: String manipulation, string methods

Wrap up, you have learned how to . . .


 Use string indexing.
• Use the square brackets [ ] to access individual or groups of
characters from a string.

 Use string manipulation functions.


• Such as length, isalpha, lower, upper, etc.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.

You might also like