4. Strings [Python 3 Programming Tutorials]

codebasics · Beginner ·🛠️ AI Tools & Apps ·7y ago

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 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
3 Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
8 Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
15 Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
20 Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
24 Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
27 Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
47 Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
48 Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
53 Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
60 Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

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
Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →