Added Homework 04 - Strings
This commit is contained in:
parent
aad4e226c5
commit
be356ccb59
24 changed files with 315 additions and 0 deletions
18
homework/homework04/strings/stringConstants01.py
Normal file
18
homework/homework04/strings/stringConstants01.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import string
|
||||
|
||||
# See https://docs.python.org/3/library/string.html
|
||||
# These are some pre-defined character variables you can use.
|
||||
|
||||
print(f"string.ascii_letters = {string.ascii_letters}")
|
||||
print(f"string.ascii_lowercase = {string.ascii_lowercase}")
|
||||
print(f"string.ascii_uppercase = {string.ascii_uppercase}")
|
||||
print(f"string.digits = {string.digits}")
|
||||
print(f"string.hexdigits = {string.hexdigits}")
|
||||
print(f"string.octdigits = {string.octdigits}")
|
||||
print(f"string.punctuation = {string.punctuation}")
|
||||
print(f"string.printable = {string.printable}")
|
||||
print(f"string.whitespace = {string.whitespace}")
|
||||
print(f"length of string.whitespace = {len(string.whitespace)}")
|
||||
|
||||
|
||||
|
||||
17
homework/homework04/strings/stringToCharacters01.py
Normal file
17
homework/homework04/strings/stringToCharacters01.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
|
||||
import string
|
||||
|
||||
message = '''
|
||||
This program prints the Ascii codes for all the letters.
|
||||
Notice the lower case letters are represented by larger
|
||||
integers than the upper case letters.
|
||||
'''
|
||||
|
||||
print(message)
|
||||
|
||||
# Load the string variable
|
||||
stringOfLetters = string.ascii_letters
|
||||
|
||||
|
||||
for myCharacter in stringOfLetters:
|
||||
print(f"{myCharacter:2s}{ord(myCharacter):3d}")
|
||||
15
homework/homework04/strings/stringToCharacters02.py
Normal file
15
homework/homework04/strings/stringToCharacters02.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
|
||||
import string
|
||||
message = '''
|
||||
This program prints the Ascii codes for all the letters
|
||||
in a text string entered by the user.
|
||||
'''
|
||||
|
||||
print(message)
|
||||
|
||||
stringOfLetters = input("Enter a name or phrase: ")
|
||||
|
||||
# Note: The ord() function returns the integer code for a character.
|
||||
|
||||
for myCharacter in stringOfLetters:
|
||||
print(f"{myCharacter:2s}{ord(myCharacter):3d}")
|
||||
31
homework/homework04/strings/stringToCharacters03.py
Normal file
31
homework/homework04/strings/stringToCharacters03.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import string
|
||||
|
||||
message = '''
|
||||
This program prints the Ascii codes for the whitespace.
|
||||
characters. These characters include the space, line return, tabs, etc.
|
||||
Printing the whitespace string does not appear to generate any results.
|
||||
Printing each character code is informative.
|
||||
'''
|
||||
message2 = '''
|
||||
Open a web browser. Go to https://www.ascii-code.com.
|
||||
Look up each of the codes from this program to determine
|
||||
their purpose.
|
||||
For example: 32 = space
|
||||
Look up the rest of the whitespace codes.
|
||||
'''
|
||||
|
||||
print(message)
|
||||
|
||||
|
||||
print(f"string.whitespace = {string.whitespace}")
|
||||
print(f"length of string.whitespace = {len(string.whitespace)}")
|
||||
# Since you cannot easily see the effect of whitespace characters,
|
||||
# we can print their numeric codes.
|
||||
|
||||
# Convert the string to a list of letters in the string.
|
||||
whiteSpaceCharactersList = list(string.whitespace)
|
||||
|
||||
for myCharacter in whiteSpaceCharactersList:
|
||||
print(f"{ord(myCharacter)}")
|
||||
|
||||
print(message2)
|
||||
21
homework/homework04/strings/stringToEmojis01.py
Normal file
21
homework/homework04/strings/stringToEmojis01.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
|
||||
# Example from https://www.geeksforgeeks.org/python-program-to-print-emojis/
|
||||
|
||||
print("grinning face ",end="")
|
||||
print("\U0001f600")
|
||||
|
||||
print("grinning squinting face ",end="")
|
||||
print("\U0001F606")
|
||||
|
||||
print("rolling on the floor laughing ",end="")
|
||||
print("\U0001F923")
|
||||
|
||||
print("grinning face ",end="")
|
||||
print("\N{grinning face}")
|
||||
|
||||
print("slightly smiling face ",end="")
|
||||
print("\N{slightly smiling face}")
|
||||
|
||||
print("winking face ",end="")
|
||||
print("\N{winking face}")
|
||||
|
||||
14
homework/homework04/strings/stringToEmojis02.py
Normal file
14
homework/homework04/strings/stringToEmojis02.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
|
||||
message = '''
|
||||
This program prints a list of emojis by character code.
|
||||
This just shows the wide range of emoji's you can
|
||||
use in your programs.
|
||||
'''
|
||||
|
||||
for i in range(127744,128360):
|
||||
print(chr(i),end=" ")
|
||||
|
||||
print("Next Set")
|
||||
|
||||
for i in range(128507,128709):
|
||||
print(chr(i),end=" ")
|
||||
Loading…
Add table
Add a link
Reference in a new issue