For all the self-taught geeks out there, here our content library with most of the learning materials we have produces throughout the years.
It makes sense to start learning by reading and watching videos about fundamentals and how things work.
Full-Stack Software Developer
Data Science and Machine Learning - 16 wks
Search from all Lessons
Curated list of small interactive and incremental exercises you can take to get better at any coding skill.
Curated section of projects to build while learning with simple instructions, videos, solutions and more.
Guides on different topics related to the technologies that we teach in our courses
Social & live learning
The most efficient way to learn: Join a cohort with classmates just like you, live streams, impromptu coding sessions, live tutorials with real experts, and stay motivated.
A bunch of letters and characters all together in a particular order, the only way to store any characters that are not a number, are a fundamental part of every major program.
Strings are also part of the most primitive or basic set of data-types:
|Number||int, float, complex|
|Sequence||list, tuple, range|
|Binary||bytes, bytearray, memoryview|
To create a string in python just put a bunch of characters within quotes like this
"hello" or even like this
1name = "Bob" 2age = "23" # <--- this is still a string (it's within quotes)
When coding a web application, everything the user types in forms it's considered a
string, even if the user types the number
2 (two) it will still be considered the string
"2" and not a real number, the developer will have to explicitely convert or parse that string into a number using the function
🔗 How to convert strings into integers with python (3 min read).
The most common use for a string is printing it using the function
1print("Hello World!") 2# The function print() receives a string and displays it on the command line/terminal.
Python allows to sum together strings using the plus
+ operator. The following fragment demonstrates how to add two strings to create a full name from first and last names.
1first_name = "Alejandro" 2last_name = "Sanchez" 3full_name = first_name + " " + last_name 4print("My name is "+full_name) 5 6# Output: "My name is Alejandro Sanchez"
In this example
"My name is " it's being concatenated with the value on the variable
You often want to know what the length (size) of a string is, for example: Twitter does not allow tweets with more than 240 characters.
1tweet = "Good morning!" 2print("The variable tweet contains "+str(len(tweet))+" characters") 3 4# Output: The variable tweet contains 13 characters
After we also need to know the value of the string in a particular position, for example: If a string ends with a question mark it's probably a question:
1question = "How are you?" 2size = len(question) 3print("The strings start with " + question) 4# Output: The strings start with H 5print("The strings ends with " + question[size - 1]) 6# Output: The strings ends with ? 7
☝️ This method of character extraction on strings is very similar to the one used on lists to extract an element from a particular position in the list.
You can also extract several characters at once. The range of the method starts with the index of the first character to be extracted and ends with the index AFTER the last character to be extracted:
1name = "My name is Alejandro Sanchez" 2print("Extracted " + name[11:20]) 3# Output: Extracted Alejandro 4 5print("Extracted " + name[11:]) 6# Output: Extracted Alejandro Sanchez 7 8print("Extracted " + nombre[:10]) 9# Output: Extracted My name is
If you want to compare two strings you can use the
== (double equal) and it will return
True if the strings are EXACTLY the same, string comparison is case sensitive, "Bob" is not equal to "bob".
1name1 = "pepe"; 2name2 = "juan"; 3if name1 == name2: 4 print("This is False, I will not get printed") 5if name1 == "pepe": 6 print("This is True, I will get printed") 7if name1 != name2: 8 print("This is True, I will get printed")
1lowercased_string = name1.lower() # will convert to lowercase 2uppercased_string = name2.upper() # will convert to uppercase
☝️ it is good practice to always lowercase strings before comparing them with others, that way we will avoid missing case sensitive differences.
1number = 3.4 # I am a number 2number_as_string = str(number) # I am a string with value "3.4"
If you want to learn more, we suggest you start practicing instead of reading because there is nothing much to read about strings, here is a small 3 min video explaining strings.