4. Strings [Python 3 Programming Tutorials]
Key Takeaways
Covers basics of strings in Python 3, including f-strings and slicing
Full Transcript
today's topic is strings in Python strings are used to store text data in Python let me show you example so let's say you want to store ice-cream and to do that you create a variable called text and using double quotation mark you type in icecream when you hit Enter what happened is this variable now contains this data called ice cream since you see on the right-hand side here in the picture the way Python stores ice cream is it's a sequence of characters so text starts at specific memory address and then after it's a sequence of characters stored in different memory addresses now using an index you can access in usual characters so for example if I want to access first character I in this variable then the way I do it is using square bracket type in text square bracket 0 if I do that I will get I so if I want to access now see you will do one so each of these initial letters are stored at specific memory locations which can be accessed using these indexes ok if now you want to let's change the content of particular character you would think what if I do this hmm it gives the error so this means strings are immutable once you create them you can change it you can place another string into this variable but you cannot partially change it using the index character so just remember that strings are immutable in Python programming languages other programming languages such as C++ Java they will allow you to do it but not Python now what if I want to access substring within this main string so let's say I want to print ice the way you do it is using the index sub range so I starts at 0 index and E and at 2 but the way sub index works is you have to give index after it so e answer to so Q 3 and when you do it you will get ice here so always remember the first index is included whereas the second one is excluded so whatever is the second index minus one that's then Dax up to which it will go and print your substring similarly if you want to print cream what you'll do is you will first figure out the index of it so it will be zero one two three four and then you will go all of it until the end so five six seven eight nine so you're getting one more here but what if you don't want to supply this index and you just want to say okay start from four and go until the end in that case you can just omit that index so just don't specify the index and it will assume that you want to go all the way towards the end similarly if you omit the index the starting index then it will start from zero so if I do this again I got iced so if you don't specify anything it will assume it's zero you can use double quotes and single quotes to store the text data so for example I can say hello like this so this is defining a string using double quotes or I can use single quote or doing the same thing they both are same thing there's just no difference this could be useful especially if you have single or double quote as part of your string itself for example if I have a string called let's learn Python so let's I will say let's long Python if I do something like this where I used single quotation to as a boundary of my string and I have a quote within my string itself if I do it it will give an error why because it is thinking that this is the end of the string whereas my intention is to make this whole thing one single string in situation like this I would always use double quotes outside and then internally you can use as many single quotes as you want and it won't give you any problem similarly if you have double code inside your string use single quote outside so let's say my hollow world has double-quote then I will use single quote outside and it just works so if you print X it will print it fine all right what if now I have multi-line string let's say I would to store my address which comprises of multiple lines and my address is let's say 1 for Wall Street New York and I want to go to the next lines as soon as I hit enter I will get this error so in this situation use triple-core so triple quotes one purple Street New York is the name of my city USA and when it ends and end it with triple quotes and now when I print this address you will see that it stored it fine without any issues now that internally the way it stores it is it will use a special character so as you if you look at internal representation of this ring it is using slash and so slash and means newline now the next thing about string is you can use plus sign to concatenate two strings so if you have a string called hello and another string called Walt and if you want to join them you just do s1 plus s2 right now here I want little space in between so you can add space like this so add a space alright so plus will just join the two strings what if I want to join number with a string so let's say I want to say totals States in USA is oops I made a mistake so total states in USA so the semester and my number is 25 so I have another variable called num where I am storing total number States and I want to join these two if I do s + num it will give me this error because Python doesn't know how to join string with an integer so what you need to do first is convert this number into a string because python can join two strings so somehow con was this convert this number to a string and then join this so there is a function called STR very important you will use this a lot in Python if you lose this it will convert 25 which was a number to a string how do I know this is string you because you see this quotation mark here so now when I do s plus STR num it will print total States in USA is 25 okay so that's it that was all about strings in Python thanks for watching
Original Description
This python 3 programming tutorial covers basics of strings. We will talk about newly introduced f-string concept in python 3.6. Also after covering string basics we will see index and strign slicing.
Exercise: https://github.com/codebasics/py/blob/master/Basics/Exercise/4_strings/4_strings_exercise.md
This tutorial's Code: https://github.com/codebasics/py/blob/master/Basics/Hindi/3_strings/3_strings.ipynb
Topics that are covered in this Video:
0:00 String introduction and index operator
0:48 String slicing
4:35 multiline string using tripple quotes (''')
5:14 \n and \t characters
5:28 + operator to join two strings
6:21 str() function
Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses.
🔖Hashtags🔖
#pythonstrings #pythonstringstutorial #pythonstring #pythonstringmethods #stringinpython #createstring #stringfunction #stringoperations #stringmethods
Next Video:
5. Lists [Python 3 Programming Tutorials]: https://www.youtube.com/watch?v=t0UTQ1eOH_g&list=PLeo1K3hjS3uv5U-Lmlnucd7gqF-3ehIh0&index=6
Website: https://codebasics.io/
Facebook: https://www.facebook.com/codebasicshub
Twitter: https://twitter.com/codebasicshub
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from codebasics · codebasics · 0 of 60
← Previous
Next →
1
2
3
4
5
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
Python Tutorial - 1. Install python on windows
codebasics
Python Tutorial - 2. Variables
codebasics
Python Tutorial - 3. Numbers
codebasics
Python Tutorial - 4. Strings
codebasics
Python Tutorial - 5. Lists
codebasics
Python Tutorial - 6. Install PyCharm on Windows
codebasics
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
Python Tutorial - 8. If Statement
codebasics
Python Tutorial - 9. For loop
codebasics
Python Tutorial - 10. Functions
codebasics
Python Tutorial - 11. Dictionaries and Tuples
codebasics
Python Tutorial - 12. Modules
codebasics
Python Tutorial - 13. Reading/Writing Files
codebasics
How to install Julia on Windows
codebasics
Python Tutorial - 14. Working With JSON
codebasics
Julia Tutorial - 1. Variables
codebasics
Julia Tutorial - 2. Numbers
codebasics
Python Tutorial - 15. if __name__ == "__main__"
codebasics
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
Python Tutorial - 16. Exception Handling
codebasics
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
Julia Tutorial - 4. Strings
codebasics
Python Tutorial - 17. Class and Objects
codebasics
Julia Tutorial - 5. Functions
codebasics
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
Julia Tutorial - 7. For While Loop
codebasics
Python Tutorial - 18. Inheritance
codebasics
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
Julia Tutorial - 10. Exception Handling
codebasics
Python Tutorial - 19. Multiple Inheritance
codebasics
Python Tutorial - 20. Raise Exception And Finally
codebasics
Python Tutorial - 21. Iterators
codebasics
Python Tutorial - 22. Generators
codebasics
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
Python Tutorial - 24. Sets and Frozen Sets
codebasics
Python Tutorial - 25. Command line argument processing using argparse
codebasics
Debugging Tips - What is bug and debugging?
codebasics
Debugging Tips - Conditional Breakpoint
codebasics
Debugging Tips - Watches and Call Stack
codebasics
Python Tutorial - 26. Multithreading - Introduction
codebasics
Git Tutorial 3: How To Install Git
codebasics
Git Tutorial 1: What is git / What is version control system?
codebasics
Git Tutorial 2 : What is Github? | github tutorial
codebasics
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
Git Github Tutorial 10: What is Pull Request?
codebasics
Git Tutorial 7: What is HEAD?
codebasics
Git Tutorial 9: Diff and Merge using meld
codebasics
Difference between Multiprocessing and Multithreading
codebasics
Python Tutorial - 27. Multiprocessing Introduction
codebasics
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
Git Tutorial 8 - .gitignore file
codebasics
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
Python Tutorial - 30. Multiprocessing Lock
codebasics
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
What is code?
codebasics
Python unit testing - pytest introduction
codebasics
More on: Python for Data
View skill →Related AI Lessons
Chapters (6)
String introduction and index operator
0:48
String slicing
4:35
multiline string using tripple quotes (''')
5:14
\n and \t characters
5:28
+ operator to join two strings
6:21
str() function
🎓
Tutor Explanation
DeepCamp AI