FOUNDATIONS OF PROGRAMMING : INTRODUCTION TO PROGRAMMING : 01.04 PROCESSING STRING VALUES

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Concatenating Strings Together

Concatenating Strings Together # Concatenation Example def main(): word1 = "mother" word2 = "board" print(word1 + word2) main() Output: motherboard Concatenate and Assign to a Variable # Concatenation Example def main(): word1 = "mother" word2 = "board" word3 = word1 + word2 print(word3) main() Output: motherboard Concatenating With Variables and String Literals # Concatenation Example def main(): word1 = "mother" word2 = "board" word3 = word1 + word2 print("The original words are " + word1 + " and " + word2 + ".") print("The new word is " + word3 + ".") main() Output: The original words are mother and board. The new word is motherboard. # Concatenation def main(): firstName = "Augusta" middleName = "Ada" lastName = "Lovelace" print("Lovelace, Augusta") main() OutPut: Lovelace, Augusta

Python's Naming Rules for Variables

Do Begin variable names with a letter or underscore. (Example: movie, movieTitle, _movieTitle) After the first letter, the variable name may contain additional letters or digits, 0 to 9. (Example: movieTitle1) Do Not Variable names may not be any of Python's keywords. Variable names may not contain spaces. (Example: movie Title) Variable names may not contain punctuation symbols. (Example: movieTitle!)

Concatenating Strings

Two or more strings can be combined to form one big string. This process is called concatenation. We use the plus symbol (+) to make this happen.

Variable Names

Valid Names: firstName amountDue randomDue time sum Invalid Names class sales tax city and state Zip Code 1stName

index position

pull out one specific character from the string; the specific position of a character in a string or an element in a list, the first index is zero.

Writing Initials

# Indexing def main(): firstName = "Augusta" middleName = "Ada" lastName = "Lovelace" print("The initials are: AAL") main() Output: The initials are: AAL

Indexing With Strings: Index Another Position

# Indexing Example def main(): word1 = "mother" word2 = "board" word3 = word1 + word2 print("The character in index 8 is " + word3[8] + ".") main() Output: The character in index 8 is a.

Indexing With Strings:

# Indexing Example: Index Zero def main(): word1 = "mother" word2 = "board" word3 = word1 + word2 print("The character in index 0 is: ") print( word3[0] ) main() Output: The character in index 0 is: m

Slicing Strings:

# Slicing Example def main(): firstName = "Augusta" middleName = "Ada" lastName = "Lovelace" print("The new name is Audalace." ) main() The new name is Audalace.

Slicing Strings: Take A Slice Out of a String

# Slicing Example def main(): word1 = "mother" word2 = "board" word3 = word1 + word2 word4 = word3[1:6] print("The new word is " + word4 + ".") main() Output: The new word is other.

Slicing Strings: Concatenate and Assign To Variable

# Slicing Example def main(): word1 = "mother" word2 = "board" word3 = word1 + word2 word4 = word3[7:10] print("The new word is " + word4 + ".") main() Output: The new word is oar.

Assigning String Values in Python

Let's say you need to remember some information, the way moviegoers tried to remember the name of the movie. Instead of storing it in your human memory, you can store information in your computer's memory using Python. This is called assigning a string value to a variable. To assign a string value to a variable in Python, follow this example. Select each part of the code to see how it works with the movie title example. movieTitle = "Live. Die. Repeat." This line of Python code is an example of an assignment statement. In an assignment statement, you tell Python, "This variable is assigned this string value."

Programmer's Good Practices

Practice: Camelcase Description: When a variable name contains two or more recognizable words or phrases, such as movieTitle or newCar, programmers use the camelcase style convention. Notice the first letter is lowercase and the first letter of each word after is capitalized. Good Example: newCar Bad Example: newcar Practice: Meaningful name Description: Always use meaningful names to avoid confusion. The names should also be self-explanatory. Good Example: newCar Bad Example: dsadsadasada Practice: Reasonable legnth Description: Keep variable names to a reasonable length. Good Example: newCar Bad Example: newCarThatIWantWhenIFinallyGraduate Practice: Use Nouns Description: Name variables with nouns, not verbs. Good Example: newCar Bad Example: driveSoFastNewCar

String Operations

You can perform different operations on string values such as concatenation, indexing, and slicing to get new string values!

Slicing Strings

a portion of a string Pulling out individual characters isn't the only thing you can do with strings. You can pull out groups, too. Individual letters aren't all that interesting. It's more likely you'll want to pull out multiple letters from a string. This can be a fun way to display "hidden" messages as you learn to code. If you put two index values in square brackets, you can take a slice out of a string. Select the parts of this code to learn how slicing works: word4 = word3[1 :6] print(word4) In this code, you're asking Python to assign the new variable word4 a part of the string "motherboard." (Remember, you assigned word3 the string value "motherboard" earlier.) Specifically, you're asking for the letters in the range 1:6. The range 1:6 makes Python look at the index positions of the characters in the string "motherboard." The letter in position 1 is "o," and the letter in position 6 is "b." Here's where it gets a little tricky: Python won't include the letter in index position 6 if you use this command. Instead, this command tells Python to stop before position 6. Therefore, when you ask Python to print word4, you should see the word "other" appear. This is a slice of a string.

Printing names

def main(): firstName = "Augusta" print(firstName) middleName = "Ada" print(middleName) lastName = "Lovelace" print(lastName) Output: Augusta Ada Lovelace

Printing Movie Titles

def main(): movieTitle = "Edge of Tomorrow" print(movieTitle) movieTitle = "Believe" print(movieTitle) main() Output: Edge of Tomorrow Believe If there are quotes around a string, Python will treat it as a string literal. Don't put quotes in the print strings.


Set pelajaran terkait

Business Law - Tradition Contract

View Set

Chapter 3-2 Measure of Variation

View Set

SPRING - QGT6 - EQ: How was the judicial system politicized against Mexican-Americans and Latinos? - Source: ZOOT SUIT (Act 2 Scenes 4-6) - chakshiri

View Set

Municipal Debt Securities -- Bond Types and Tax Treatment

View Set

Cognitive Psychology: Chapter 3: Visual Perception

View Set