Strings in Python - Advanced Python 05 - Programming Tutorial
Key Takeaways
The video tutorial covers advanced Python concepts, specifically focusing on strings, including creation, manipulation, concatenation, and formatting, using various methods and operators such as +, %, str.format(), and f-strings.
Full Transcript
hi everybody welcome to a new Python tutorial today we're gonna talk about strings in Python a string is an ordered and immutable collection data type that is used for text representation and it is one of the most used data types in Python so I hope that at the end of the session you'll feel comfortable working with them so let's start first of all a string is created with either single or double quotes so you can use double quotes and then put your letters in here so let's say hello world and now we can print this and then we see our string here or you can use single quotes this is probably more common and the only thing you have to be careful is if you have another single quote inside this so if you have for example I am a programmer now if you try to run this then this will get a syntax error so what you can do is you can either use an escaping backslash here so this is valid or you can put your single quote inside double quotes so this is again a valid string you may also sometimes see triple quotes so this is typically used for multi-line strings so now I can go in another line and this is also used for documentation inside your code so now if we run this we see that our string goes over multiple lines now you may also sometimes see in escaping backslash like so and this just says that the string should continue in another line but it should not create a new line here so now if we run this then we see we have our one line hello world string now if you want to access characters or substrings it's the same like with lists and you access it with inside brackets so let's say char equal or let's create the string first all right my string equals hello world and then you can say char equals and then my string and then in brackets you put the index you want so if you want the very first character you have to use index zero so we can print this so this is the age and if we use index 1 we get the e and so on we can also use a negative index so minus 1 is the very last character - to the second last and so on but what we cannot do for example is we cannot access a character and change it so if I want to change the first character to a lower age if I try to run this now this will get a type or a string object does not support item assignment and this is because strings are immutable so they cannot be changed so be aware of that and we can also access a whole substring with slicing so then I will say my string and in brackets I put this start index so let's say 1 and then a colon and then a stop index and then if I print this then I will see I get the string e llo so what this does it starts at index 1 and goes until index 5 but index 5 is excluded so be careful here so it has one two three and four so our string is e LLO now if I don't use a start index then it starts all the way from the beginning and if I don't use a stop index then it goes all the way to the end so this goes all the way from beginning to end and then there's another optional step index so if I put another coat on here and by default this is one so this takes every character and now if I put a two here it takes every second character and I can also put a minus 1 here and then what it does it will reverse our string so that's a nice little trick to reverse the string with this slicing operator now we can concatenate two or more strings simply with a plus so if I create another string Tom and I will just call this hello and say this is now a greeting and then I can say my sentence I will create a new string that is greeting plus and then I want a space between them and then plus again plus the name and now let's print this and then we see we have our concatenated string so very easy with this plus here now we can iterate over our string with a for in loop so for I in greeting and then do something so just print is print every letter so this goes over our whole string and prints each character and we don't have to call this I we can also call this for example X or whatever we want now we want to check if a character or substring is inside our string we can do this with an if in statement so I say if and then I want to check for the letter e so if e in greeting and then I will print yes and otherwise else I will print no so E is inside my word so it prints yes so if I check for example for P then it will print no and I can also check for substring so I can check for e ll this will also print yes now let's talk about some more useful methods that you can do with string so let's say we have a string with some white space here and then we have our hello world and then some more white space at the end so if we print this we will see that our printed string also has the white string now if I want to get rid of this I can do my string equals my string dot strip so this method removes our whitespace so now if I print it we see that the whitespace is gone and be aware that we this method does not change our string in place because as I said a string is immutable so if I just write it like this then this will not change my original string so if I run it our original string still has the whitespace so what we have to do is we have to assign it again to our original one and then we have the new string with without whitespace now what we can do also with strings is we can say we can convert every character to an uppercase so let's say my string dot upper and then we have all in upper cases we can also say my string dot lower then we have all in lower cases we can check if my string starts with a specific character or substring so if we can say starts with and then we can say H so this will give us true or we can also check for hello also true and there's a check with world and we will get a false that we can also check if it ends with something so if it ends with world so now we have true and yeah if it ends with hello then we get a false now we can find the index of a character or a substring so let's say my string dot find and then we want to find po so this will return the first index that it finds with an O so index 0 1 2 3 4 so it returns a 4 we can also check for sub strings so this is the at index 3 our l.o substring starts and if it does not find a string then it will return a minus 1 we can also count the number of characters or substring it finds so let's check for how many O's we have in hello world so this will return - and how many peas do we have we have 0 we can also replace characters or sub strings inside our string so we can say my string that replace and then we want to replace world with a new word so we want to replace it with universe and now if you print this and we see our string is now hello universe and also be aware here that this will return a new string and does not change this one so if it does not find the strings let's say for example we have a typo here then it does nothing so it will still print the original string hello world now let's talk about lists and strings so let's say you have a string with some words so let's say we have here how are you doing and you want to convert this to a list and put at each word of my string as an element in my list now what you can do then is you can say my list equals my string dot split and if we print our list then we see that we have each word now as an element in our list and by default that the limiter it is looking for is a space so here the default argument is a space so it looks for each space and then splits our string here now for example if you have commas here and then cannot find a space so it we only have one element here so now what you would then have to use you would have to use as a delimiter a comma and then again we have four elements now if you have the list and you want to convert it back into a string what you can do is you can say let's say new string equals and then we say my we know we say an empty string and then dot join and then the list as an argument and then we print the new string and then we will see that this will concatenate all of our words all of our elements in our list so this will put all of our elements together as a string and between each element it will put the string that we put here so now if you put a space here then it will put a space between each element and now again we have our original string again so that that joint must method is a very useful method to quickly join the elements of a list back into a string and I would highly recommend to remember this one because this is very useful and let's in fact let's talk about this a little bit more so let's say we have a list with some elements so let's say for example only ace and then x six so maybe you know the syntax so this will create a list with six elements and now if you have two tasks to join this into a string a lot of times what you will see is that you will create an empty string and then you will use a four in loop so for I in my list and then you will say my string plus equals I so let's check this so it worked we have our string here but this is bad Python code because what happens here since a string is immutable this will create a new string here and then assign it back to our original string so this operation is very expensive what you should better use is the dub join method so as I just showed you we can say my string equals and then we will say an empty string dot join my list and then we will print this so this will also give us the same thing but it's much cleaner and also much faster so let's look at the time of both of these ways so let's say we say from the time add module we import the default timer as timer and then we will say here start equals timer and at the end we can say stop equals timer and then we will print stop - start so this will give us the time it takes from here to here and we will do the same thing here so if we run this we will let's remove this we see that both both was very very fast but now let's say we have for example a very large list with let's say 1 million elements and now I don't want to print my list so if you run this now what we will see that this the second way where the dot join method is much faster so the first way it took more than half a second and the second only point 0 1 seconds so forget this way of doing it and remember the duck join method now as the last thing I want to talk about formatting strings so there are two ways to format a string the old styles are with a % operator or with a thought formant method and since python 3.6 there's also the new f strings and let's talk about all of these methods so let's say we have a variable and call it and it's let's say variable equals a string and we have to name tomm and then we will create a string and say it's the variable is and then we use the first method so we used a percent s and then after our string again we use the percent and then the variable so this tells the interpreter that we have a placeholder with a string here and then afterwards we fill this placeholder with our variable so now if you print our variable then we will see that our string is the variable is Tom now if we have a number here we shouldn't use person s here we should use percent d so this stands for an integer decimal value so now we have the variable is 3 and let's say we have a floating point like this and if we run this then we see that we still have a 3 here because we told the Python that we have a decimal value here so now what we want now is we want a floating point so we say % F and then we have a floating point value here and by default it has 6 digits after the decimal point so if we want to specify how many digits we want to have we can say dot % a dot and then how many digits and then let's say 2 digits and then dot 2f so this will give us two digits after the decimal point so this is the very old formatting style the new formatting style is with the dot formant method so now what you want to do is as a placeholder we use races and then after our string we call the dot format method and then here we put all our elements as arguments so now if we print this then we see that we have the placeholder got replaced with our variable and we can also specify and how many digits so we can say : dot 2f so then we have two digits after the decimal point and for example if we have more variables we simply would place another placeholder here and then another argument here so let's say we have var 2 equals 6 and then we would put var 2 here and then we will see that we have all our variables inside our string now so these are the old formatting styles and the newest way to do it is with the F string so this is since python 3.6 or newer you can use the f strings and with an F string you would simply put a F between the string and then the string and then you would also use braces and inside the braces you can use your variables directly so you can use var here and var 2 here and then you don't need this anymore so if we run this then we see it worked and yeah I think this is much more readable it's more concise and it's even faster especially if you have a lot of variables here so I would highly recommend using this F string F strings now since Python 3.6 and yeah what this does is it evaluates the this at runtime so we can also put some operation here so let's say is a mathematical operation like VAR times 2 and then this will will be evaluated at runtime so now we see we have our 2 times our variable here and yeah so that's it about F strings and that's all I wanted to show you about the strings I hope you enjoyed this tutorial and see you in the next tutorial where we talk about collections in Python
Original Description
Strings in Python - Advanced Python 05 - Programming Tutorial
In this Python Advanced Tutorial, we will be learning about Strings in Python. A String is an ordered, and immutable collection data type that is used for text representation, and it's one of the most used data types in Python. We will go over how you can use them and some advanced techniques that can be applied to strings.
~~~~~~~~~~~~~~ GREAT PLUGINS FOR YOUR CODE EDITOR ~~~~~~~~~~~~~~
✅ Write cleaner code with Sourcery: https://sourcery.ai/?utm_source=youtube&utm_campaign=pythonengineer *
📚 Get my FREE NumPy Handbook:
https://www.python-engineer.com/numpybook
📓 Notebooks available on Patreon:
https://www.patreon.com/patrickloeber
⭐ Join Our Discord : https://discord.gg/FHMg9tKFSN
A written Tutorial can be found here:
https://www.python-engineer.com/courses/advancedpython/05-strings/
You can find me here:
Website: https://www.python-engineer.com
Twitter: https://twitter.com/patloeber
GitHub: https://github.com/patrickloeber
#Python
----------------------------------------------------------------------------------------------------------
* This is a sponsored or an affiliate link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Patrick Loeber · Patrick Loeber · 5 of 60
1
2
3
4
▶
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Lists in Python - Advanced Python 01 - Programming Tutorial
Patrick Loeber
Tuples in Python - Advanced Python 02 - Programming Tutorial
Patrick Loeber
Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Patrick Loeber
Sets in Python - Advanced Python 04 - Programming Tutorial
Patrick Loeber
Strings in Python - Advanced Python 05 - Programming Tutorial
Patrick Loeber
Collections in Python - Advanced Python 06 - Programming Tutorial
Patrick Loeber
Itertools in Python - Advanced Python 07 - Programming Tutorial
Patrick Loeber
Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Patrick Loeber
Exceptions in Python - Advanced Python 09 - Programming Tutorial
Patrick Loeber
Logging in Python - Advanced Python 10 - Programming Tutorial
Patrick Loeber
JSON in Python - Advanced Python 11 - Programming Tutorial
Patrick Loeber
Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Patrick Loeber
Decorators in Python - Advanced Python 13 - Programming Tutorial
Patrick Loeber
Generators in Python - Advanced Python 14 - Programming Tutorial
Patrick Loeber
Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Patrick Loeber
Threading in Python - Advanced Python 16 - Programming Tutorial
Patrick Loeber
Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Patrick Loeber
Function arguments in detail - Advanced Python 18 - Programming Tutorial
Patrick Loeber
The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
Patrick Loeber
Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Patrick Loeber
Context Managers in Python - Advanced Python 21 - Programming Tutorial
Patrick Loeber
KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
Patrick Loeber
Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Patrick Loeber
Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Patrick Loeber
Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Patrick Loeber
Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Patrick Loeber
Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Patrick Loeber
SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
Patrick Loeber
Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Patrick Loeber
Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Patrick Loeber
Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Patrick Loeber
PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
Patrick Loeber
K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
Patrick Loeber
Anaconda Tutorial - Installation and Basic Commands
Patrick Loeber
PyTorch Tutorial 01 - Installation
Patrick Loeber
PyTorch Tutorial 02 - Tensor Basics
Patrick Loeber
PyTorch Tutorial 03 - Gradient Calculation With Autograd
Patrick Loeber
PyTorch Tutorial 04 - Backpropagation - Theory With Example
Patrick Loeber
PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
Patrick Loeber
PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
Patrick Loeber
PyTorch Tutorial 07 - Linear Regression
Patrick Loeber
PyTorch Tutorial 08 - Logistic Regression
Patrick Loeber
PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
Patrick Loeber
PyTorch Tutorial 10 - Dataset Transforms
Patrick Loeber
Download Images With Python Automatically - Python Web Scraping Tutorial
Patrick Loeber
PyTorch Tutorial 11 - Softmax and Cross Entropy
Patrick Loeber
Select Movies with Python - Web Scraping Tutorial
Patrick Loeber
PyTorch Tutorial 12 - Activation Functions
Patrick Loeber
List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
Patrick Loeber
PyTorch Tutorial 13 - Feed-Forward Neural Network
Patrick Loeber
How To Add A Progress Bar In Python With Just One Line - Python Tutorial
Patrick Loeber
PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
Patrick Loeber
The Walrus Operator - New in Python 3.8 - Python Tutorial
Patrick Loeber
PyTorch Tutorial 15 - Transfer Learning
Patrick Loeber
YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
Patrick Loeber
YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
Patrick Loeber
YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
Patrick Loeber
YouTube Data API Tutorial with Python - Analyze the Data - Part 4
Patrick Loeber
AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
Patrick Loeber
Ultimate FREE Study Guide for Machine Learning and Deep Learning
Patrick Loeber
More on: Prompt Craft
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
X now offers an MCP server to make its platform easier for AI tools to use
TechCrunch AI
n8n Automation Repurpose Video Content: The 2025 Production Guide
Dev.to AI
You’re Still Paying $200/Month for AI Tools You Could Replace With a Free Local Setup Tonight
Medium · Data Science
Top 10 AI Tools Every College Student Should Know in 2026
Medium · AI
🎓
Tutor Explanation
DeepCamp AI