Strings
Strings are a data type in Python for
dealing with text.
Python has a number of powerful
features for manipulating strings.
Creating a string
A string is created by enclosing text in
quotes.
You can use either single quotes, ', or
double quotes, ".
A triple-quote can be used for multi-line
strings.
E.g.
Getting string Input from user
when getting numerical input we use an
eval statement with the input
statement,
but when getting text, we do not use
eval.
Empty string
The empty string, '' is the string
equivalent of the number 0.
It is a string with nothing in it.
We have seen it before, in the print
statement’s optional argument, sep=''.
String length – len()
To get the length of a string
(how many characters it has),
use the built-in function len().
String Repetition
The * operator repeats a string for a
certain number of times.
Example
A program to build a string of only vowels
given a string from the user.
User enters a string of length, say 8
characters, letter by letter.
The in operator
The in operator is used to tell if a string
contains something.
For example:
Challenge
Write a program that accepts a name from
the user and checks if there is a letter ‘e’ or
‘i’ in the name. It should print accordingly.
Solution
Combining in and not
E.g. to tell if a string does not contain
something:
Indexing
Picking out individual characters from a
string.
Python uses square brackets [ ] to do this.
E.g. Indexing the string s='Python‘
Negative indices count backwards from
position [0].
Slicing
Picking out a substring from the string.
A combination of indexing and the
range() function.
o Starts at the 1st arg,
o Ends at the last arg minus one
E.g. s='abcdefghij'.
Looping
scanning through a string one character
at a time.
A for loop like the one below can be
used to do that.
Task
Write a program that accepts a word/name
from the user and prints the vowels in that
word/name.
Solution
String methods
Strings come with a ton of methods.
The methods return information about the
string.
Or they return a new string that is a modified
version of the original.
Here are some of the most useful ones
upper()
If you want to change a string, s, to
upper case, it is not enough to just use
s.upper().
This shows that python strings are
immutable.
o You cannot change the original
string.
s.replace()
replaces 1st arg in string with 2nd arg
e.g.
Escape characters
The backslash, \, is used to get certain
special characters, called escape
characters, into your string.
E.g. suppose you want to insert an
apostrophes into strings. You might
encounter errors;
To get around this error, use an escape
character ‘\’ so that the next character
after the backslash will not be
interpreted literally. i.e. as is used in
strings.
New line character ‘\n’
One of the most used escape character
is the new line or next line character, \n
It is used to advance to the next line
when a string is written in a single line.
e.g.
Task
1. Write a program that asks the user for
a string and prints out the index of
each 'a‘ in the string.
Solution
2. Write a program that asks the user for
a string and creates a new string that
doubles each character of the original
string. For instance, if the user enters
Hello, the output should be HHeelllloo.
Solution