title
How to Use Strings in Python - Python Tutorial for Beginners
description
How to use strings in Python. Learn Python basics with this Python tutorial for beginners.
🔥Subscribe for more Python tutorials like this: https://goo.gl/SjXTT7
🔥Python tutorial for beginners: https://youtu.be/yE9v9rt6ziw
Python Exercises for Beginners:
https://programmingwithmosh.com/python/python-exercises-and-questions-for-beginners/
Python Cheat Sheet:
https://programmingwithmosh.com/python/python-3-cheat-sheet/
Want to learn more from me? Check out my blog and courses:
http://programmingwithmosh.com
https://www.facebook.com/programmingwithmosh/
https://twitter.com/moshhamedani
detail
{'title': 'How to Use Strings in Python - Python Tutorial for Beginners', 'heatmap': [{'end': 338.173, 'start': 319.694, 'weight': 0.764}, {'end': 503.742, 'start': 481.752, 'weight': 0.742}, {'end': 570.559, 'start': 523.145, 'weight': 0.759}, {'end': 636.539, 'start': 582.842, 'weight': 0.719}, {'end': 817.505, 'start': 780.876, 'weight': 0.75}], 'summary': 'Tutorial covers python programming basics, built-in functions, string manipulation techniques, and string methods, emphasizing the usage of quotes, including single, double, and triple quotes for formatting and code deletion, along with operations on strings. it also delves into accessing characters, slicing strings, escape sequences, concatenation, and formatted strings for construction, and highlights the flexibility and functionality of python string methods.', 'chapters': [{'end': 56.193, 'segs': [{'end': 56.193, 'src': 'embed', 'start': 4.198, 'weight': 0, 'content': [{'end': 7.239, 'text': 'So here we have this course variable set to Python programming.', 'start': 4.198, 'duration': 3.041}, {'end': 12.741, 'text': 'As I told you before, whenever you work with text, you should surround your text with quotes.', 'start': 7.859, 'duration': 4.882}, {'end': 15.962, 'text': 'You can either use double quotes or single quotes.', 'start': 13.301, 'duration': 2.661}, {'end': 20.364, 'text': "That's more of a personal preference, but quite often we use double quotes.", 'start': 16.242, 'duration': 4.122}, {'end': 26.59, 'text': 'we also have triple quotes and we use them to format a long string.', 'start': 21.204, 'duration': 5.386}, {'end': 28.672, 'text': "for example, if you have, let's say,", 'start': 26.59, 'duration': 2.082}, {'end': 41.778, 'text': 'a variable message that is the message we want to include in the body of an email you can use triple quotes to format it like this hi, John,', 'start': 28.672, 'duration': 13.106}, {'end': 46.964, 'text': 'this is Marsh from code with marsh.com, blah, blah, blah.', 'start': 41.778, 'duration': 5.186}, {'end': 50.007, 'text': "so that's when we use triple quotes.", 'start': 46.964, 'duration': 3.043}, {'end': 53.671, 'text': "now we don't need this in this lecture, so delete.", 'start': 50.007, 'duration': 3.664}, {'end': 56.193, 'text': 'let me show you a few useful things you can do with strings.', 'start': 53.671, 'duration': 2.522}], 'summary': 'Python programming course covers string usage and formatting.', 'duration': 51.995, 'max_score': 4.198, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/Ctqi5Y4X-jA/pics/Ctqi5Y4X-jA4198.jpg'}], 'start': 4.198, 'title': 'Python programming basics', 'summary': 'Discusses the use of quotes in python programming, focusing on single, double, and triple quotes, with an emphasis on the usage of triple quotes for formatting long strings and deletion of code, as well as useful operations on strings.', 'chapters': [{'end': 56.193, 'start': 4.198, 'title': 'Python programming basics', 'summary': 'Discusses the use of quotes in python programming, including single quotes, double quotes, and triple quotes, with a focus on the usage of triple quotes for formatting long strings, and deletion of code. it also briefly touches on the useful operations that can be performed on strings.', 'duration': 51.995, 'highlights': ['The chapter emphasizes the use of single quotes, double quotes, and triple quotes in Python programming, with a preference for double quotes, and highlights the usage of triple quotes for formatting long strings.', 'It briefly mentions the deletion of unnecessary code in the lecture.', 'The chapter also mentions the demonstration of useful operations that can be performed on strings in Python programming.']}], 'duration': 51.995, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/Ctqi5Y4X-jA/pics/Ctqi5Y4X-jA4198.jpg', 'highlights': ['The chapter emphasizes the use of single quotes, double quotes, and triple quotes in Python programming, with a preference for double quotes, and highlights the usage of triple quotes for formatting long strings.', 'The chapter also mentions the demonstration of useful operations that can be performed on strings in Python programming.', 'It briefly mentions the deletion of unnecessary code in the lecture.']}, {'end': 552.955, 'segs': [{'end': 103.491, 'src': 'embed', 'start': 57.114, 'weight': 0, 'content': [{'end': 62.98, 'text': 'First of all, we have this built in function in Python for getting the length of strings.', 'start': 57.114, 'duration': 5.866}, {'end': 69.767, 'text': 'What is a function? A function is basically a reusable piece of code that carries out a task.', 'start': 63.601, 'duration': 6.166}, {'end': 73.13, 'text': 'As a metaphor, think of the remote control of your TV.', 'start': 70.388, 'duration': 2.742}, {'end': 75.112, 'text': 'This remote control.', 'start': 74.071, 'duration': 1.041}, {'end': 80.636, 'text': 'you have buttons for different functions like turn on, turn off, change the channel and so on.', 'start': 75.112, 'duration': 5.524}, {'end': 86.499, 'text': 'These are the built-in functions in your TV, in Python and many other programming languages.', 'start': 80.636, 'duration': 5.863}, {'end': 92.283, 'text': 'We have the exact same concept So we have functions that are built into the language on the platform.', 'start': 86.539, 'duration': 5.744}, {'end': 96.346, 'text': 'You can reuse these functions to perform various tasks.', 'start': 92.584, 'duration': 3.762}, {'end': 103.491, 'text': 'So here we can use the built-in len function to get the length of a string, which means the number of characters in that string.', 'start': 96.346, 'duration': 7.145}], 'summary': 'Python has a built-in len function to get string length.', 'duration': 46.377, 'max_score': 57.114, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/Ctqi5Y4X-jA/pics/Ctqi5Y4X-jA57114.jpg'}, {'end': 247.11, 'src': 'embed', 'start': 217.503, 'weight': 2, 'content': [{'end': 221.764, 'text': "Now let's say we want to extract the first three characters in this string.", 'start': 217.503, 'duration': 4.261}, {'end': 223.705, 'text': 'So here we need two indexes.', 'start': 222.424, 'duration': 1.281}, {'end': 228.346, 'text': 'Start index, colon the end index.', 'start': 224.705, 'duration': 3.641}, {'end': 235.487, 'text': 'So this will return a new string that contains the first three characters in this course variable.', 'start': 228.346, 'duration': 7.141}, {'end': 238.288, 'text': 'That will be P, Y and T.', 'start': 235.487, 'duration': 2.801}, {'end': 242.289, 'text': 'So the index of these characters are 0, 1 & 2.', 'start': 238.288, 'duration': 4.001}, {'end': 247.11, 'text': 'So that means the character at the end index is not included.', 'start': 242.289, 'duration': 4.821}], 'summary': 'Extract the first three characters from a string using indexes.', 'duration': 29.607, 'max_score': 217.503, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/Ctqi5Y4X-jA/pics/Ctqi5Y4X-jA217503.jpg'}, {'end': 350.746, 'src': 'heatmap', 'start': 319.694, 'weight': 3, 'content': [{'end': 328.037, 'text': 'we use bracket notation to get access to a specific element or a specific character, and We use this notation to a slice a string.', 'start': 319.694, 'duration': 8.343}, {'end': 338.173, 'text': 'So we have this string here Python programming.', 'start': 335.891, 'duration': 2.282}, {'end': 343.158, 'text': "Now let's say we want to put a double quote in the middle of this string.", 'start': 338.934, 'duration': 4.224}, {'end': 344.84, 'text': 'There is a problem here.', 'start': 343.939, 'duration': 0.901}, {'end': 350.746, 'text': 'Python interpreter sees this second string as the end of the string.', 'start': 345.721, 'duration': 5.025}], 'summary': 'Using bracket notation to access elements and slice strings in python.', 'duration': 31.052, 'max_score': 319.694, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/Ctqi5Y4X-jA/pics/Ctqi5Y4X-jA319694.jpg'}, {'end': 522.946, 'src': 'heatmap', 'start': 481.752, 'weight': 4, 'content': [{'end': 486.08, 'text': 'We get a new line after Python, so programming will end up on the second line.', 'start': 481.752, 'duration': 4.328}, {'end': 489.988, 'text': 'So these are the escape sequences in Python.', 'start': 487.603, 'duration': 2.385}, {'end': 500.201, 'text': 'Here we have two variables, first and last.', 'start': 497.861, 'duration': 2.34}, {'end': 503.742, 'text': "Let's say we want to print my full name on the console.", 'start': 500.882, 'duration': 2.86}, {'end': 516.705, 'text': 'So we can define another variable, full, set it to first, then concatenate it with a space, and one more time concatenate it with last.', 'start': 504.582, 'duration': 12.123}, {'end': 522.946, 'text': 'Now when we print full, we get my full name on the console.', 'start': 517.625, 'duration': 5.321}], 'summary': 'Learning python escape sequences and string concatenation.', 'duration': 35.343, 'max_score': 481.752, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/Ctqi5Y4X-jA/pics/Ctqi5Y4X-jA481752.jpg'}], 'start': 57.114, 'title': 'Python built-in functions and string manipulation techniques', 'summary': "Discusses python's built-in functions, with a focus on the len function for string length retrieval, and covers string manipulation techniques including accessing specific characters, slicing strings, using escape sequences, and string concatenation. it also introduces the usage of formatted strings for string construction.", 'chapters': [{'end': 128.195, 'start': 57.114, 'title': 'Python built-in functions', 'summary': 'Discusses the concept of built-in functions in python, focusing on the len function, which is used to retrieve the length of a string and the metaphor of a function as a reusable piece of code, like a tv remote control.', 'duration': 71.081, 'highlights': ['The concept of built-in functions in Python is explained, emphasizing their reusability and their similarity to the functions in a TV remote control.', 'The len function in Python is highlighted as a built-in function used to retrieve the length of a string.', 'Functions are defined as reusable pieces of code that carry out specific tasks, with the metaphor of a TV remote control used to illustrate their purpose.']}, {'end': 552.955, 'start': 128.195, 'title': 'Working with strings in python', 'summary': 'Covers string manipulation techniques, such as obtaining string length, accessing specific characters, slicing strings, using escape sequences, and string concatenation in python. it also introduces the usage of formatted strings as a newer approach for string construction.', 'duration': 424.76, 'highlights': ['Obtaining string length using len function', 'Accessing specific characters using square bracket notation', 'Slicing strings to extract specific parts', 'Usage of escape sequences in Python strings', 'String concatenation and introduction to formatted strings']}], 'duration': 495.841, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/Ctqi5Y4X-jA/pics/Ctqi5Y4X-jA57114.jpg', 'highlights': ['The len function in Python is highlighted as a built-in function used to retrieve the length of a string.', 'Obtaining string length using len function', 'Slicing strings to extract specific parts', 'Accessing specific characters using square bracket notation', 'Usage of escape sequences in Python strings', 'Functions are defined as reusable pieces of code that carry out specific tasks, with the metaphor of a TV remote control used to illustrate their purpose.']}, {'end': 1002.931, 'segs': [{'end': 641.222, 'src': 'heatmap', 'start': 582.842, 'weight': 1, 'content': [{'end': 585.422, 'text': 'We get the exact same result.', 'start': 582.842, 'duration': 2.58}, {'end': 591.304, 'text': 'Just be aware that you can put any valid expressions in between curly braces.', 'start': 585.422, 'duration': 5.882}, {'end': 595.165, 'text': 'So earlier you learned about the built-in len function.', 'start': 591.304, 'duration': 3.861}, {'end': 600.186, 'text': 'We can call len here to get the length of this string.', 'start': 595.165, 'duration': 5.021}, {'end': 601.747, 'text': "Let's run this program one more time.", 'start': 600.186, 'duration': 1.561}, {'end': 608.493, 'text': 'So we get 4 and We can also replace last with an expression like this, two plus two.', 'start': 601.807, 'duration': 6.686}, {'end': 611.895, 'text': "Let's run this program, we get four and four.", 'start': 609.254, 'duration': 2.641}, {'end': 619.357, 'text': 'So when using formatted strings, you can put any valid expressions in between curly braces.', 'start': 613.095, 'duration': 6.262}, {'end': 636.539, 'text': "In this lecture, we're going to look at a few useful functions available to work with strings So earlier you learned about this built-in len function.", 'start': 627.935, 'duration': 8.604}, {'end': 641.222, 'text': "This function is general purpose So it's not limited to strings later.", 'start': 637.2, 'duration': 4.022}], 'summary': 'Using formatted strings, valid expressions can be placed in curly braces to get results.', 'duration': 28.127, 'max_score': 582.842, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/Ctqi5Y4X-jA/pics/Ctqi5Y4X-jA582842.jpg'}, {'end': 780.876, 'src': 'embed', 'start': 670.736, 'weight': 0, 'content': [{'end': 671.557, 'text': 'for now,', 'start': 670.736, 'duration': 0.821}, {'end': 682.642, 'text': 'what I want you to take away is that everything in Python is an object and objects have functions we call methods that we can access using the dot notation.', 'start': 671.557, 'duration': 11.085}, {'end': 685.483, 'text': 'So here, course is an object.', 'start': 683.542, 'duration': 1.941}, {'end': 690.926, 'text': 'we use the dot notation to access its functions or, more accurately, methods.', 'start': 685.483, 'duration': 5.443}, {'end': 692.607, 'text': "Let's take a look at a few of these methods.", 'start': 690.926, 'duration': 1.681}, {'end': 697.19, 'text': 'We have upper to convert a string to uppercase.', 'start': 693.388, 'duration': 3.802}, {'end': 701.612, 'text': "Now let's print this and run the program.", 'start': 697.19, 'duration': 4.422}, {'end': 703.833, 'text': 'Here is what we get Beautiful.', 'start': 701.612, 'duration': 2.221}, {'end': 707.794, 'text': 'now note that the methods that you call here return a new string.', 'start': 703.833, 'duration': 3.961}, {'end': 715.717, 'text': 'So the original string is not affected Let me show you so print course Run the program one more time.', 'start': 708.194, 'duration': 7.523}, {'end': 723.259, 'text': 'Look, this is our original string, right? So course the upper returns a new string a new value.', 'start': 716.337, 'duration': 6.922}, {'end': 733.152, 'text': 'We can store it in a variable like course underline capital and Like this now to keep this demo simple and consistent.', 'start': 723.739, 'duration': 9.413}, {'end': 742.878, 'text': "I'm gonna revert this back and use a print statement We also have the lower method to convert a string to lowercase.", 'start': 733.232, 'duration': 9.646}, {'end': 749.342, 'text': 'I also have Title which will capitalize the first letter of every word.', 'start': 743.599, 'duration': 5.743}, {'end': 758.969, 'text': 'So if our string was like this When we call the title method, we get Python programming, as you see here.', 'start': 749.823, 'duration': 9.146}, {'end': 768.252, 'text': 'Okay, Another useful method is strip, and we use it to trim any white space at the beginning or end of a string.', 'start': 759.609, 'duration': 8.643}, {'end': 771.893, 'text': 'This is particularly useful when we receive input from the user.', 'start': 768.252, 'duration': 3.641}, {'end': 773.553, 'text': 'Let me show you so.', 'start': 772.473, 'duration': 1.08}, {'end': 780.876, 'text': "let's imagine The user entered a couple of white spaces at the beginning of this string.", 'start': 773.553, 'duration': 7.323}], 'summary': 'Python objects have methods like upper, lower, title, and strip for string manipulation.', 'duration': 110.14, 'max_score': 670.736, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/Ctqi5Y4X-jA/pics/Ctqi5Y4X-jA670736.jpg'}, {'end': 817.505, 'src': 'heatmap', 'start': 780.876, 'weight': 0.75, 'content': [{'end': 786.438, 'text': 'When we call course that strip, those white spaces will be removed.', 'start': 780.876, 'duration': 5.562}, {'end': 786.918, 'text': 'Take a look.', 'start': 786.438, 'duration': 0.48}, {'end': 794.281, 'text': 'So note that in the first three examples we have those white spaces, but in the last one it is removed.', 'start': 788.118, 'duration': 6.163}, {'end': 799.303, 'text': 'So a strip removes the white space from both the beginning and end of a string.', 'start': 794.961, 'duration': 4.342}, {'end': 806.647, 'text': 'We also have L strip, which is short for left strip and R strip, which is short for right strip.', 'start': 799.803, 'duration': 6.844}, {'end': 809.388, 'text': 'So it will remove the white space from the end of a string.', 'start': 806.947, 'duration': 2.441}, {'end': 817.505, 'text': 'If you want to get the index of a character or a sequence of characters in your string, you should use the find method.', 'start': 810.6, 'duration': 6.905}], 'summary': 'String methods like strip and find remove white spaces and locate characters.', 'duration': 36.629, 'max_score': 780.876, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/Ctqi5Y4X-jA/pics/Ctqi5Y4X-jA780876.jpg'}, {'end': 961.411, 'src': 'embed', 'start': 928.147, 'weight': 5, 'content': [{'end': 932.149, 'text': "but as this expression returns a boolean, so it's a true or false.", 'start': 928.147, 'duration': 4.002}, {'end': 935.31, 'text': 'Let me show you so run the program.', 'start': 932.149, 'duration': 3.161}, {'end': 948.497, 'text': 'we get the boolean true and Finally we have the not operator and we use that to see if our string does not contain a character or sequence of characters.', 'start': 935.31, 'duration': 13.187}, {'end': 953.46, 'text': "so Let's change this to Swift.", 'start': 948.497, 'duration': 4.963}, {'end': 957.507, 'text': 'not, in course.', 'start': 955.004, 'duration': 2.503}, {'end': 961.411, 'text': "when this expression is evaluated, what do you think we're going to get?", 'start': 957.507, 'duration': 3.904}], 'summary': 'Demonstrating boolean operations in swift with examples.', 'duration': 33.264, 'max_score': 928.147, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/Ctqi5Y4X-jA/pics/Ctqi5Y4X-jA928147.jpg'}], 'start': 553.755, 'title': 'Python string methods', 'summary': 'Explains the usage of curly braces for formatted strings and introduces various python string methods, showcasing the flexibility and functionality of the built-in len function and emphasizing the concept of python objects and their methods.', 'chapters': [{'end': 641.222, 'start': 553.755, 'title': 'Using curly braces for formatted strings', 'summary': 'Explains the usage of curly braces for formatted strings, demonstrating how to print variable values and perform expressions within the braces, showcasing the flexibility and functionality of the built-in len function, and emphasizing the general-purpose nature of the function.', 'duration': 87.467, 'highlights': ['The chapter explains the usage of curly braces for formatted strings, demonstrating how to print variable values and perform expressions within the braces, showcasing the flexibility and functionality of the built-in len function, and emphasizing the general-purpose nature of the function.']}, {'end': 1002.931, 'start': 641.282, 'title': 'Python string methods', 'summary': 'Introduces various string methods in python, including upper, lower, title, strip, find, replace, in, and not in operators, emphasizing the concept that everything in python is an object with functions called methods.', 'duration': 361.649, 'highlights': ['The chapter introduces various string methods in Python, including upper, lower, title, strip, find, replace, in, and not in operators.', 'Emphasizes the concept that everything in Python is an object with functions called methods.', 'Demonstrates the usage of these methods through practical examples and explains the impact of these methods on the original string.', 'Provides practical examples of using these methods, such as converting a string to uppercase, lowercase, and capitalizing the first letter of every word.', 'Introduces the concept of using the in and not in operators to check for the existence of characters or sequences in a string.']}], 'duration': 449.176, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/Ctqi5Y4X-jA/pics/Ctqi5Y4X-jA553755.jpg', 'highlights': ['The chapter introduces various string methods in Python, including upper, lower, title, strip, find, replace, in, and not in operators.', 'The chapter explains the usage of curly braces for formatted strings, demonstrating how to print variable values and perform expressions within the braces, showcasing the flexibility and functionality of the built-in len function, and emphasizing the general-purpose nature of the function.', 'Emphasizes the concept that everything in Python is an object with functions called methods.', 'Demonstrates the usage of these methods through practical examples and explains the impact of these methods on the original string.', 'Provides practical examples of using these methods, such as converting a string to uppercase, lowercase, and capitalizing the first letter of every word.', 'Introduces the concept of using the in and not in operators to check for the existence of characters or sequences in a string.']}], 'highlights': ['The chapter introduces various string methods in Python, including upper, lower, title, strip, find, replace, in, and not in operators.', 'The chapter emphasizes the use of single quotes, double quotes, and triple quotes in Python programming, with a preference for double quotes, and highlights the usage of triple quotes for formatting long strings.', 'The len function in Python is highlighted as a built-in function used to retrieve the length of a string.', 'The chapter explains the usage of curly braces for formatted strings, demonstrating how to print variable values and perform expressions within the braces, showcasing the flexibility and functionality of the built-in len function, and emphasizing the general-purpose nature of the function.', 'The chapter also mentions the demonstration of useful operations that can be performed on strings in Python programming.', 'Emphasizes the concept that everything in Python is an object with functions called methods.', 'Provides practical examples of using these methods, such as converting a string to uppercase, lowercase, and capitalizing the first letter of every word.', 'Demonstrates the usage of these methods through practical examples and explains the impact of these methods on the original string.', 'Obtaining string length using len function', 'Slicing strings to extract specific parts', 'Accessing specific characters using square bracket notation', 'Usage of escape sequences in Python strings', 'Introduces the concept of using the in and not in operators to check for the existence of characters or sequences in a string.', 'It briefly mentions the deletion of unnecessary code in the lecture.', 'Functions are defined as reusable pieces of code that carry out specific tasks, with the metaphor of a TV remote control used to illustrate their purpose.']}