0% found this document useful (0 votes)
2 views32 pages

8.3 String Manipulation

The document provides an overview of string manipulation in Python, highlighting built-in methods for tasks such as checking string properties, formatting, and modifying strings. It includes lesson objectives, key terms, and examples of various string methods like concatenation, length checking, and case conversion. The content aims to help learners practice and understand string manipulation techniques effectively.

Uploaded by

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

8.3 String Manipulation

The document provides an overview of string manipulation in Python, highlighting built-in methods for tasks such as checking string properties, formatting, and modifying strings. It includes lesson objectives, key terms, and examples of various string methods like concatenation, length checking, and case conversion. The content aims to help learners practice and understand string manipulation techniques effectively.

Uploaded by

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

Procedural Python:

String Manipulation

Computer Science UK www.computerscienceuk.com


Starter
Study the code below…

How could we work out how many characters the string contains?
How could we find the location of the first ‘s’?
How could we replace the word ‘simple’ with ‘short’?

…all may take a good few lines of code! But thankfully, the python programming
language has some useful built-in methods, allowing these tasks (and many more) to be
coded with a single line of code

Computer Science UK www.computerscienceuk.com


Lesson Objectives
Lesson Objectives
• Understand that the python programming language has many
built-in string manipulation methods
• Learn how to make use of a variety of string manipulation methods.

Lesson Outcomes
• Carry out a number of programming tasks to practice programming
with string manipulation methods.

Literacy – Key Words

String A data type which consists of an arrangement of characters.


Strong Manipulation Useful built-in programming methods, which can process strings in order to change or
Methods use the string’s data.

Computer Science UK www.computerscienceuk.com


Introduction to String Manipulation
Introduction

String manipulation is very useful and very widely used in every language.

Often, programmers are required to break down strings and examine them closely.

For example, a password strength checker would have to strip a string down and examine
them closely to see if it contains letters, numbers and/or symbols.

This presentation will take a look at the various methods of manipulating strings, that are built
into Python.

We can perform a great number of useful tasks on strings via these methods.

Computer Science UK www.computerscienceuk.com


Python String Methods (String Manipulation)
Some examples of methods that we can perform include:

String Checks:
- Find the length of a string
- Find out if a string is all in uppercase
- find out if a string is all in lowercase
- find out if a string’s words all start with capitals letters
- Check whether a string is alphanumeric(contains both letters and numbers)
- Check whether a string contains only numbers
- Check whether a string contains only letters
- Check whether a string only contains spaces String Formatting Methods:
- Find the location of a character in a string - Concatenation
- Count how many times a character appears in a string - Turn a string upper case
- Turn a string lower case
String to Lists Conversions: - Capitalize a string (make first letter capital)
- Split a string into a number of items on a list - Capitalize each word in a string
- Swap the case of each letter in a string around
- Add spaces in either side of a string.

Other Useful String Methods:


- Return the character at a certain position of a string
- Replace letters and words in a string

Let’s now explore some of these methods…

Computer Science UK www.computerscienceuk.com


Concatenation
Concatenation simply means ‘joining’.

We know from before that the ‘+’ operator is used to add numeric data types together.

This operator has a different function when it deals with strings. The ‘+’ sign simply joins
together two or more string.

Example:
full_name_no_space = firstname + surname

Full_name = firstname + “ ” + surname

https://coder.computerscienceuk.com/coder/r094/

Computer Science UK www.computerscienceuk.com


Find the length of a string
The len() method:

This helps us find the length of a string


(how many characters it has – including spaces)

Or

https://coder.computerscienceuk.com/coder/vgZ0/

Computer Science UK www.computerscienceuk.com


Access a character of string at a given position…

from left

string[3]

from right

string[-3]
https://coder.computerscienceuk.com/coder/wjr1/

Computer Science UK www.computerscienceuk.com


Look at each character of string in turn

https://coder.computerscienceuk.com/coder/xG13/

Computer Science UK www.computerscienceuk.com


Find out if a string is all in uppercase

https://coder.computerscienceuk.com/coder/zmXZ/

Computer Science UK www.computerscienceuk.com


Find out if a string is all in lowercase

https://coder.computerscienceuk.com/coder/An13/

Computer Science UK www.computerscienceuk.com


Check whether a string is alphanumeric (contains only letters and numbers)

https://coder.computerscienceuk.com/coder/Bg6k/

Computer Science UK www.computerscienceuk.com


Check whether a string contains only letters

https://coder.computerscienceuk.com/coder/DR8y/

Computer Science UK www.computerscienceuk.com


Check whether a string contains only number

https://coder.computerscienceuk.com/coder/Nx8D/

Computer Science UK www.computerscienceuk.com


Find out if a string’s words all start with capitals letters

https://coder.computerscienceuk.com/coder/O7ZY/

Computer Science UK www.computerscienceuk.com


Check whether a string only contains spaces

https://coder.computerscienceuk.com/coder/P1Y1/

Computer Science UK www.computerscienceuk.com


Turn a string upper case

OR

https://coder.computerscienceuk.com/coder/Q1O0/

Computer Science UK www.computerscienceuk.com


Turn a string lower case

OR

https://coder.computerscienceuk.com/coder/RgLO/

Computer Science UK www.computerscienceuk.com


Capitalise a string (make first letter capital)

OR

https://coder.computerscienceuk.com/coder/VmNv/

Computer Science UK www.computerscienceuk.com


Capitalize each word in a string

OR

https://coder.computerscienceuk.com/coder/Wn8Q/

Computer Science UK www.computerscienceuk.com


Swap the case of each letter in a string around

OR

https://coder.computerscienceuk.com/coder/X6M5/

Computer Science UK www.computerscienceuk.com


Add spaces to indent or centre strings on screen.

Returns 15 character length, version of the string,


right justified. In this example, the string is 4
characters long, so the new string will have 11
white spaces.

https://coder.computerscienceuk.com/coder/Y6MM/

Returns 14 character length, version of the string,


centre aligned. In this example, the string is 4
characters long, so the new string will have 5
white spaces either side.

https://coder.computerscienceuk.com/coder/Z4Ww/

Computer Science UK www.computerscienceuk.com


Pretty Printing
string.format() method

Here is an example of how the


variables (contained in the format
function’s brackets) is printed
according to some pretty printing
rules.

For digits:
{0:2d} {1:3d} {2:4d} means, in the first
column (0) allow for 2 digits, in the https://coder.computerscienceuk.com/coder/1j9V/
second column (1) allow for 3 digits
etc.

For strings:
{0:10} means, in the first column,
allow for 10 characters etc.

https://coder.computerscienceuk.com/coder/2R91/

Computer Science UK www.computerscienceuk.com


Split a string into a number of items on a list

This helpful method will split up a


string, adding each set of characters
that lie either side of a space, into a
list data structure.

https://coder.computerscienceuk.com/coder/0g9y/

Computer Science UK www.computerscienceuk.com


Split a string into 2 items on a list around a specific letter

Similarly, this helpful method will split


up a string, adding each set of
characters that lie either side of a
given substring, into a list data
structure.

https://coder.computerscienceuk.com/coder/g5kG/

Computer Science UK www.computerscienceuk.com


Return the first few letters/numbers/characters of the string from the left

This method will return a substring, of a


given length, from the left of the
string.

https://coder.computerscienceuk.com/coder/3l9O/

Computer Science UK www.computerscienceuk.com


Return the last few letters/numbers/characters of the string from the right

This method will return a substring, of a


given length, from the right of the
string.

https://coder.computerscienceuk.com/coder/489V/

Computer Science UK www.computerscienceuk.com


Return a certain number of letters/numbers/characters within the string

This method will return a substring, that starts with the


character at the index location of the first provided value,
followed by all characters after that, up to (but not
including) the second provided index location.

https://coder.computerscienceuk.com/coder/58WK/

Computer Science UK www.computerscienceuk.com


Replace letters and words in a string

https://coder.computerscienceuk.com/coder/982B/

Computer Science UK www.computerscienceuk.com


Count how many times a character appears in a string

https://coder.computerscienceuk.com/coder/8q9g/

Computer Science UK www.computerscienceuk.com


Find the location of a character in a string

https://coder.computerscienceuk.com/coder/660V/

Computer Science UK www.computerscienceuk.com


Lesson Activity

• Read the information in the lesson’s workbook

• Complete the PRIMM tasks provided.

Computer Science UK www.computerscienceuk.com

You might also like