3 OperationsWithString
3 OperationsWithString
Python
Sokratis Sofianopoulos
Basic String Operations
Indexing strings
• Strings are bits of text. They can be defined as anything between quotes:
astring = "Hello world!"
astring2 = 'Hello world!'
• Printing the length of a string
print(len(astring))
• Printing the location of the first occurrence of a character
print(astring.index("o"))
4
startswith and endswith
• This is used to determine whether the string starts with something or ends
with something, respectively
• print(astring.startswith("Hello"))
• This ill print True, as the string starts with "Hello“
• print(astring.endswith("World"))
• What will this print?
5
count
6
find
In the string astring = "Hello world!" you need to find the index of the second
appearance of the "o" character. How can you do that if you only know that
there are two "o" characters in the string but you do not know the index of
the first character?
8
Solution
9
isdigit() and isalpha()
10
String replacing
11
String replace example
12
Getting a slice of a string
13
Extended slice syntax
14
Transforming to uppercase or lowercase
15
Splitting a string
16
String Formatting
String formatting
18
C-style String Formatting
name = "Sokratis"
print("Hello, %s!" % name)
19
C-style String Formatting (cont.)
21
Solution
# testing code
if mystring == "hello":
print("String: %s" % mystring)
if isinstance(myfloat, float) and myfloat == 10.0:
print("Float: %f" % myfloat)
if isinstance(myint, int) and myint == 20:
print("Integer: %d" % myint)
22
Basic argument specifiers
• %s
• String (or any object with a string representation, like numbers)
• %d
• Integers
• %f
• Floating point numbers
• %.<number of digits>f
• Floating point numbers with a fixed amount of digits to the right of the dot
• %x/%X
• Integers in hex representation (lowercase/uppercase)
23
New Style String Formatting
24
f-Strings
• Python 3.6 added a new formatting approach: formatted string literals or f-strings
• This new way of formatting strings lets you use embedded Python expressions
inside string constants
• Example:
name = "Sokratis"
print(f"Hello, {name}!")
25
f-String examples
labname = "Lab"
labnumber = 510
day="Monday"
print(f"Lecture is at {labname} {labnumber} every {day}")
26
Comparing with a traditional string creation
a=5
b = 10
message = f'Five plus ten is {a + b} and not {2 * (a + b)}.'
27
Escaping characters when using f-Strings
• Make sure you are not using the same type of quotation mark on the outside of
the f-string as you are using in the expression (mix double and single ones)
• If you find you need to use the same type of quotation mark on both the inside
and the outside of the string, then you can escape with the backslash (\)
character:
name = "Sokratis"
print(f"Hello, \"{name}\"!")
• You can work around this by evaluating the expression beforehand and using the
result in the f-string:
name = '"Sokratis"'
print(f"Hello, {name}!")
28
Exercises
1. Write the following message combining three variables using f-String:
2. Using two variables a and b write the following message using f-String:
A in the power of B is AB
29
Solution
1. name = "Sokratis"
lname = "Sofianopoulos"
balance = 53.44
print(f'Hello "{name} {lname}". Your current balance is {balance}€')
2. a = 2
b=3
print(f"{a} in the power of {b} is {a**b}")
30