String Variables and Asking A User To Enter A Value
String Variables and Asking A User To Enter A Value
subtitle style
String variables and asking a user to enter a value
input
name = input("What is your name? ")
name
Chris
If you need to remember more than one value, just
create more variables
name favoriteMovie
Real
Chris
Genius
city
Pasadena
You can access the value you stored later in your
code
name = input("What is your name? ")
print(name)
You can also change the value of a variable later in
the code
name = input("What is your name? ")
print(name)
name = "Mary"
print(name)
DEMO
Accessing a value entered by a user
Click to edit Master
subtitle style
• Rules
– Can not contain spaces
– Are case sensitive
• firstName and firstname would be two different variables
– Cannot start with a number
• Guidelines
– Should be descriptive but not too long (favoriteSign not
yourFavoriteSignInTheHoroscope)
– Use a casing "scheme"
• camelCasing or PascalCasing
Which of the following do you think would be good
names for variables?
• Variable1
• First Name
• Date
• 3Name
• DOB
• DateOfBirth
• YourFavoriteSignInTheHoroscope
Click to edit Master
subtitle style
Manipulating variables
You can combine variables and strings with the +
symbol
firstName = input("What is your first name? ")
lastName = input("What is your last name? " )
print("Hello" + firstName + lastName)
Often you need to add punctuation or spaces to
format the output correctly
firstName = input("What is your first name? ")
lastName = input("What is your last name? " )
print("Hello " + firstName + " " + lastName)
DEMO
Formatting output
Now you can create a story teller program!
animal = input("What is your favorite animal? " )
building = input("Name a famous building: ")
color = input("What is your favorite color? ")
print("Hickory Dickory Dock!")
print("The "+color+" "+animal+" ran up the "+building)
Variables also allow you to manipulate the contents
of the variable
message = 'Hello world'
print(message.lower())
print(message.upper())
print(message.swapcase())
DEMO
Manipulating values with string functions
Geek Tip!
That’s IntelliSense.
message = 'Hello world'
print(message.find('world'))
print(message.count('o'))
print(message.capitalize())
print(message.replace('Hello','Hi'))
Programmers do not memorize all these functions!!
Have a user enter their postal code and then display that postal
code in upper case letters even if the user typed it in lowercase?
postalCode = input("Please enter your postal code: ")
print(postalCode.upper())
DEMO
Converting to uppercase
Did you notice?
The intellisense didn’t appear to help us select the upper() function.
Ask someone for their name and then display the name
someone with the first letter of their first and last name
uppercase and the rest of their name lowercase?
name = " "
name = input("Please enter your name: ")
print(name.capitalize())
Functions and variables allow us to make new
mistakes in our code…
message = Hello world message = 'Hello world'
23message = 'Hello world' 23message = 'Hello world'
New message = 'Hi there' New message = 'Hi there'
print(message.upper) print(message.upper())
print(mesage.lower()) print(message.lower())
print(message.count()) print(message.count('H'))
Your challenge