Skip to document

Task 4 String Manipulation - Procedural Pytho

yer
Module

Cambridge Ordinary Level Islamiat (2058)

5 Documents
Students shared 5 documents in this course
Academic year: 2024/2025
Uploaded by:
0followers
2Uploads
0upvotes

Comments

Please sign in or register to post comments.

Preview text

2

r
Computer Science UK Membership Site Licence: Do not share outside of your centre

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.

We will now 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.

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:

coder.computerscienceuk/coder/r094/

Length of Strings

The len() method helps us find the length of a string (i. how many characters it has –

including spaces)

coder.computerscienceuk/coder/vgZ0/

r

Accessing Characters of a String

We can access characters of string at a given position, using square brackets:

from left

string[3]

from right

string[-3]

coder.computerscienceuk/coder/wjr1/

Looking at each character of string in turn – using a loop:

coder.computerscienceuk/coder/xG13/

Uppercase Checks

To find out if a string is all in uppercase, we can perform the following:

coder.computerscienceuk/coder/zmXZ/

Lowercase Checks

To find out if a string is all in lowercase, we can perform the following:

coder.computerscienceuk/coder/An13/

Alphanumeric Check

r

coder.computerscienceuk/coder/P1Y1/

Case Conversion (lower to upper)

In order to turn a string into upper case, we can use the upper() method:

coder.computerscienceuk/coder/Q1O0/

Case Conversion (upper to lower)

In order to turn a string into lower case, we can use the lower() method:

coder.computerscienceuk/coder/RgLO/

Capitalise Strings

To capitalise a string (make first letter of the string a capital), we can use the

capitalize() method:

coder.computerscienceuk/coder/VmNv/

Capitalise Every Word of a Strings

To capitalise every word of a string (make first letter of each word in a string a

capital), we can use the title() method:

r

coder.computerscienceuk/coder/Wn8Q/

Case Swap

In order to swap the case of each letter in a string, we can use the swapcase()

method:

coder.computerscienceuk/coder/X6M5/

Indenting / Centring Strings

In order to add spaces, to indent or centre strings on screen, we can use the following

methods:

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.

coder.computerscienceuk/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.

coder.computerscienceuk/coder/Z4Ww/

Pretty Printing

We can use the .format() method to better organise string data on the screen:

r

In order to return the first few characters of a string, from the left we can perform the

following.

This method will return a substring, of a given length, from the left of the string:

https://

coder.computerscienceuk/coder/3l9O/

This method will return a substring, of a given length, from the right of the string:

coder.computerscienceuk/coder/489V/

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:

coder.computerscienceuk/coder/58WK/

Replace Characters

Ro replace letters and words in a string, we can use the replace() method:

coder.computerscienceuk/coder/982B/

Counting Characters in a String

r

If we need to count how many times a character appears in a string, we can use the

count() method:

coder.computerscienceuk/coder/8q9g/

Locating a Character in a String

If we need to find the location of a character in a string, we can use the find() method:

coder.computerscienceuk/coder/660V/

PRIMM TASKS

1: Predict

r

What happens if,

password() is

removed?

Explain the result.

4: Modify

Modify the code so

that, in addition to

only accepting a

password which

contains both letters

and numbers, it also

only accepts

passwords that are

between 6 and 12

characters long.

Add a screenshot of

your code and explain

how the code works

line by line.

5: Make

Create a program in python for each point below and add a screenshot of your code

in the boxes provided:

r

1) Create a program which will return the length of a user inputted string.

2) Create a program which will ask the user to enter a sentence, and then convert all

inputted characters to lowercase.

3) Create a program that will ask the user to enter a string containing the letter ‘z’. The

program will repeatedly reject any inputs which do not.

4) Create a program which asks the user to enter a string of 10 characters (rejecting all

other inputs), and then converts the first 5 characters to lowercase, converts the last 5

characters to uppercase, before displaying the manipulated string back to the screen.

Was this document helpful?

Task 4 String Manipulation - Procedural Pytho

Module: Cambridge Ordinary Level Islamiat (2058)

5 Documents
Students shared 5 documents in this course
Was this document helpful?
2
ComputerScienceUK.com
CSUK:Teache
r
Computer Science UK Membership Site Licence: Do not share outside of your centre
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.
We will now 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.
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:
https://coder.computerscienceuk.com/coder/r094/
Length of Strings
The len() method helps us find the length of a string (i.e. how many characters it has –
including spaces)
https://coder.computerscienceuk.com/coder/vgZ0/