8.3 String Manipulation
8.3 String Manipulation
String Manipulation
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
Lesson Outcomes
• Carry out a number of programming tasks to practice programming
with string manipulation methods.
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.
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.
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
https://coder.computerscienceuk.com/coder/r094/
Or
https://coder.computerscienceuk.com/coder/vgZ0/
from left
string[3]
from right
string[-3]
https://coder.computerscienceuk.com/coder/wjr1/
https://coder.computerscienceuk.com/coder/xG13/
https://coder.computerscienceuk.com/coder/zmXZ/
https://coder.computerscienceuk.com/coder/An13/
https://coder.computerscienceuk.com/coder/Bg6k/
https://coder.computerscienceuk.com/coder/DR8y/
https://coder.computerscienceuk.com/coder/Nx8D/
https://coder.computerscienceuk.com/coder/O7ZY/
https://coder.computerscienceuk.com/coder/P1Y1/
OR
https://coder.computerscienceuk.com/coder/Q1O0/
OR
https://coder.computerscienceuk.com/coder/RgLO/
OR
https://coder.computerscienceuk.com/coder/VmNv/
OR
https://coder.computerscienceuk.com/coder/Wn8Q/
OR
https://coder.computerscienceuk.com/coder/X6M5/
https://coder.computerscienceuk.com/coder/Y6MM/
https://coder.computerscienceuk.com/coder/Z4Ww/
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/
https://coder.computerscienceuk.com/coder/0g9y/
https://coder.computerscienceuk.com/coder/g5kG/
https://coder.computerscienceuk.com/coder/3l9O/
https://coder.computerscienceuk.com/coder/489V/
https://coder.computerscienceuk.com/coder/58WK/
https://coder.computerscienceuk.com/coder/982B/
https://coder.computerscienceuk.com/coder/8q9g/
https://coder.computerscienceuk.com/coder/660V/