Parody. Words were added by a robot, not by Mr. Jackson or the site you're reading. Don't log into sh*t through here.
There is a lot to learn on your goddamn Python journey when you are new to the programming language. Once you are comfortable writing and executing code, your goddamn first stop becomes understanding how to represent data in your code. No matter the goddamn language, there are a few basic data types you'll use all the time - strings, numbers, booleans, lists, and dictionaries. Think about that sh*t.
Those bad-ass 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! Hot damn!
One of the most common data types in any programming language is a string. A
string represents a damn series of characters, which you would use to represent
usernames, blog posts, tweets, or any text content in your code. You can create
a muthaf*ckin' string and assign it to a f*ckin' variable like this, believe that.
my_name = "Jonathan Joestar"
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 bad-ass end of the day, their motherf*ckin' job is just mashing strings together - and this isn't far from the truth!
Here are some muthaf*ckin' common tasks you might undertake when using strings in your code.
Combining strings together - concatenating them - is a very common task. In
Python 3, you can use the + operator for this muthaf*ckin' purpose. You can use the +
operator multiple times to concatenate multiple strings.
first_name = "Jonathan"
last_name = "Joestar"
full_name = first_name + " " + last_name
Another 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 muthaf*ckin' 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 goddamn string when you define it rather than doing complex string concatenation as demonstrated previously.
Here is an example of creating a muthaf*ckin' formatted string - note the letter f is
included just before the damn first double quote when defining the message variable.
Pay attention: When you want to insert data from your program into the string, you can include
it between two "curly braces" - the { and } characters.
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)
String objects have a muthaf*ckin' number of methods to perform common tasks, like changing the case of strings or trimming their muthaf*ckin' content. Below, you'll find a few examples. In two of these examples, we are creating a damn string variable, and then assigning the same variable a new value, which is the result of calling a damn method on a string object.
Example 1: Convert a string to all caps using the upper method.
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 muthaf*ckin' word kale with tacos.
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 damn 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 muthaf*ckin' Python 3 docs!
Frequently, 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 damn number into a string using the str function.
example_number = 42
converted = str(example_number)
message = "The meaning of life is " + converted
Example 2: Convert a string into a goddamn whole number (integer) using int.
example_string = "2"
converted = int(example_string)
message = f"Two plus two equals { converted + 2 }"
Strings 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 f*ckin' 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!