GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.fullstackpython.com — Basic Data Types in Python 3: Strings Basic Data Types in Python 3: Strings
Posted by
Kevin Whinnery on October 18, 2019.
There is a lot to learn on your Python journey when you are new to the programming language. Once you are comfortable writing and executing code, your first stop becomes understanding how to represent data in your code. No matter the language, there are a few basic data types you'll use all the time - strings, numbers, booleans, lists, and dictionaries. Those data types, and how to use them in Python 3, are the topic of this blog post series. Today, we're starting with strings. If you're learning Python, you might also want to check out TwilioQuest 3. You'll learn about basic data types and much more about Python programming. Ready to learn how to use strings in Python 3? Let's get started! Strings in Python 3One of the most common data types in any programming language is a my_name = "Jonathan Joestar" Strings are "immutable"In Python, strings are considered immutable - once you create them, they can't be changed. You can, however, use a variety of methods to create new strings from existing strings. This type of work in programming is called string manipulation. Some web developers joke that at the end of the day, their job is just mashing strings together - and this isn't far from the truth! Here are some common tasks you might undertake when using strings in your code. Common task - combining strings togetherCombining strings together - concatenating them - is a very common task. In
Python 3, you can use the first_name = "Jonathan" last_name = "Joestar" full_name = first_name + " " + last_name Common task - inserting data into stringsAnother common task with strings is inserting data into a specific place within a string. In programming, we call this string interpolation. Python 3 provides a handy tool for doing this called "f" strings. The "f" in "f strings" stands for format - you can insert other data from your program into a string when you define it rather than doing complex string concatenation as demonstrated previously. Here is an example of creating a formatted string - note the letter first_name = "Jonathan"
last_name = "Joestar"
age = 24
message = f"My name is {first_name} {last_name}, and I am {age} years old."
print(message)
Common task - using built-in string methods to manipulate stringsString objects have a number of methods to perform common tasks, like changing the case of strings or trimming their content. Below, you'll find a few examples. In two of these examples, we are creating a string variable, and then assigning the same variable a new value, which is the result of calling a method on a string object. Example 1: Convert a string to all caps using the example_string = "am I stoked enough yet?"
example_string = example_string.upper()
print(example_string) # prints "AM I STOKED ENOUGH YET?"
Example 2: Replace all instances of the word example_string = "We're having kale for dinner! Yay kale!"
example_string = example_string.replace("kale", "tacos")
print(example_string) # prints "We're having tacos for dinner! Yay tacos!"
Example 3: Split a comma-delimited string into a list of strings. example_string = "Apples,Oranges,Pears"
groceries = example_string.split(',')
# Code below prints:
# Apples
# Oranges
# Pears
for item in groceries:
print(item)
Check our more strings can do in the Python 3 docs! Type castingFrequently, you will want to convert data from one type into another. In programming, we call this process type casting. There are a number of functions built in to Python which allow us to do these type conversions on basic data types. Example 1: Convert a number into a string using the example_number = 42 converted = str(example_number) message = "The meaning of life is " + converted Example 2: Convert a string into a whole number (integer) using example_string = "2"
converted = int(example_string)
message = f"Two plus two equals { converted + 2 }"
Wrapping upStrings of text are one of the most common pieces of data you will work with in programming. Hopefully, you've learned a bit about how to work with strings in Python 3! Stay tuned for more blog posts in this series to learn more about basic data types like strings, numbers, booleans, lists, and dictionaries. Also, be sure to download and play TwilioQuest 3 to learn even more about Python! |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |