I originally published this post on LinkedIn: https://www.linkedin.com/pulse/devasc-200-901-python-strings-riikka-sihvonen/
This is a summary based on the DEVASC 200-901 Official Cert Guide.
The string data type is a sequence of characters inside quotes. You can use single quotes (' ') or double quotes (" "). A string can also include numbers and other characters. However, some characters in Python strings have special use cases.
Example: Create a string.
>>> word = 'Python'
In Python, you don't have to explicitly specify the data type, when you assign a value to an object. Python automatically recognizes the type from the value.
A string is just a list of characters in a certain order that Python keeps track of. Strings are indexed starting from 0. You can retrieve any character in the string by referencing its index.
Example: Find the first character in the string.
>>> word[0]
>>> 'P'
You can also pull ranges of characters by using a colon (:). The format is [x:y], where x is the beginning of the slice and y determines the end. Note that the second number (y) is considered "up to but not including", so the last character to be retrieved is the one before the specified index. You can also leave out the start index (to print from the start of the string) or leave out the end index (to print out the end of the string).
Example: Find the characters from index 1 to 4 ("up to but not including").
>>> word[1:4]
>>> 'yth'
You can also use negative numbers to indicate the index. If you use a negative number, you start from the end at position -1.
Example: Find the last character in the string.
>>> word[-1]
>>>'n'
You can perform math operations on strings as well. The plus (+) operator is used to add or concatenate two strings together.
>>> 'Python' + 'Space'
>>> 'PythonSpace'
Multiplication (*) works as well.
>>> 'Fun! ' * 3
>>> 'Fun! Fun! Fun! '
There are many built-in methods in Python that allow you to manipulate the string in different ways.
Example: Convert all characters in the string to upper case.
>>> word.upper()
>>> 'PYTHON'
See: https://docs.python.org/3/library/stdtypes.html (String Methods)
Watch my YouTube video DEVASC - Introduction to Python Strings here:
Python comes up with several built-in data structures like Strings, Lists, Tuples, Sets, Dictionaries etc. Keep sharing your valuable knowledge and expertise. Thank you for sharing this. Great blog. data structure tutorial
ReplyDeleteThank you so much for sharing this blog with us. It provides a collection of useful information. You obviously put a lot of effort into it! Best python list comprehension service provider.
ReplyDeleteI really appreciate your information which you shared with us. If anyone who want to create his/her career in python So Contact Here-+91-9311002620 Or Visit our website https://www.htsindia.com/Courses/python/python-training-institute-in-south-delhi
ReplyDelete
ReplyDeleteStudying for 200-901 Study Material can be overwhelming, but the well-structured content in the
200-901 Practice Test study material makes it manageable and even enjoyable.