Python Strings
Lesson 7 of 10 in Coddy's Social media Search Project - Python JSON Fundamentals course.
Before we move on to our next task, we need to learn about Python Strings. It is presumed that you understand this from the python fundamentals course. If you don't remember, let's recap.
Python strings are basically arrays of characters.
print("Hello world")For example, the above code prints a string Hello world on to our standard output.
It is enclosed with double-quotes. However, we can use single-quotes as well, like the below example which returns the same output:
print('Hello world')Having to deal with strings is very common in the real world.
There are a ton of methods that can be used to analyze and manipulate strings.
Common string use cases
Here are some common use cases and comments alongside each line of code
s = "I LOVE Python strings" # assigns the string literal to the variable 's'
print(s) # prints to the standard output: I LOVE Python strings
print(len(s)) # prints the length of the string. => 21
# Strings are arrays, so characters can be accessed using index
print(s[0]) # prints the value - I index starts from 0
print(s[-1]) # prints the value - s (s[-1] = last character of the string, s[-2] = second last character)
# You can slice a string from index i to index j-1 using s[i:j]
print(s[0:4]) # prints the value - I LO (Slices the string from position 0 to position 4 (not including position 4).
# Remember that space is also a character
print(s[2:6] # prints the value - LOVE (Slicing from position 2 to position 6 - not including position 6)
# Concatenation
a = "Hello"
b = "World"
s = a + b # this concatenates string a and string b anc creates a new string s = "Hello World"Splitting strings
The split() method splits the original string into a list of substrings. This takes in a separator character as parameter.
Whenever this separator or the end of string is found, a substring up to that point is sliced and added to the resulting list of strings.
s = "apple,orange,mango"
fruits = s.split(",") # Constructs a list of substrings (fruits) from the original string s
print(fruits) # Prints the list to the standard output: ["apple", "orange", "mango"]Formatting strings
Python has a format() method that's very useful when constructing dynamic strings.
# This is pretty easy. In this example we want to print the following:
# Employee Name: Rodrick. Employee Id: 51.
# However the values "Rodrick" and "51" could change.
# Step 1: Make sure you have the dynamic values ready.
name = "Rodrick"
emp_id = 51 # note that the dynamic values doesn't necessarily have to be strings
# Step 2: Define the static string with placeholders on the dynamic parts as {}
s = "Employee Name: {}. Employee Id: {}."
# Step 3: Apply the format function
print(s.format(name, emp_id))This is not an exhaustive list of abilities of Python strings.
Take the python fundamentals course to learn python strings in depth.
Try it yourself
This lesson doesn't include a code challenge.