Tt
Click this widget to change the font size.
CC
Click this widget to change contrast.

Home Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Links | Search | Bio |

Guide to Python (University at Buffalo Version)


Chapter 8: Working With Strings

Python Strings

Python provides many methods to maniplulate strings. Strings are not mutable. Below are a few examples.

# string_for.py

myString = "Welcome to Python!"
for c in myString:
  print(c)
Figure 8-1: Walk Through a String One Character at a Time Code
W
e
l
c
o
m
e
 
t
o
 
P
y
t
h
o
n
!
Figure 8-2: Walk Through a String One Character at a Time Output
# string_count.py

myString = "Welcome to Python!"
print(f"The Letter o appears {myString.count('o')} times.")
   
Figure 8-3: String count() Method Code
The Letter o appears 3 times.
Figure 8-4: String count() Method Output
# string_methods.py

myString = "Welcome to Python!"
print(f"myString = {myString}")
lower = myString.lower();
print(f"using lower = {lower}")
upper = myString.upper();
print(f"using upper = {upper}")
cap = myString.capitalize();
print(f"using capitalize = {cap}")
  
Figure 8-5: String capitalize()upper(), and upper() Methods Code
MyString = Welcome to Python!
using lower = welcome to python!
using upper = WELCOME TO PYTHON!
using capitalize = Welcome to python!
  
Figure 8-6: String capitalize(), upper(), and upper() Methods Code

Formatting Python Strings Using the format() Method

Python provides many Formatting types for usng the format() method to format strings. Below are a few examples.

print("Using placeholders")
str1  = "My name is {firstName}, I teach in the {dept}"
str1 += " Department.".format(firstName = "Jim", dept = "CIS")
str2  = "My name is {0}, I teach in the {1}"
str2 += " Department.".format("Jim", "CIS")
flowersArray = ["Rose", "Red", 19.95]
largeNumber = 123456789.12
print("str1 =", str1)
print("str2 =", str2)
print("largeNumber =", largeNumber)
print("-----")
print("Left Aligning data using the :> characters")
print("-----")
print(f"| {flowersArray[0]:>10} | {flowersArray[1]:>10} | {flowersArray[2]:>5} |")
print("-----")
print("Right Aligning data using the :< characters")
print(f"| {flowersArray[0]:<10} | {flowersArray[1]:<10} | {flowersArray[2]:<5} |")
print("-----")
print("Center Aligning data using the :^ characters")
print(f"| {flowersArray[0]:^10} | {flowersArray[1]:^10} | {flowersArray[2]:^5} |")
print("-----")
print("Formatting the currency")
print(f"| {flowersArray[0]:^10} | {flowersArray[1]:^10} | ${flowersArray[2]:>5.2f} |")
print("-----")
print("Formatting a number")|")
print(f"{largeNumber:,.2f}")
  
Figure 8-7: Using the String format() Method Code
Using placeholders
str1 = My name is Jim, I teach in the CIS Department.
str2 = My name is Jim, I teach in the CIS Department.
largeNumber = 123456789.12
-----
Left Aligning data using the :> characters
|       Rose |        Red | 19.95 |
-----
Right Aligning data using the :< characters
| Rose       | Red        | 19.95 |
-----
Center Aligning data using the :^ characters
|    Rose    |    Red     | 19.95 |
-----
Formatting the currency
|    Rose    |    Red     | $19.95 |
-----
Formatting a number
123,456,789.12
  
Figure 8-8: Using the String format() Method Output

Help contribute to my OER Resources. Donate with PayPal button via my PayPal account.
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2025 Jim Gerland