Saturday, April 18, 2015

Python String Basics - Python Basics - Python Tutorial

Python StringPython String


In today’s Python tutorial, we are going to look at another important data type that we use often in Python. We are about to focus on a data type called a string. A string is a sequence of data like text or a collection of bytes. In this tutorial, we are going to only be focusing on an introduction to the Python string. We will dig deeper into strings in chapter 4 of this series.


What is a String?


A string contains a list of characters in a specific order(sequence). The characters in a string can be letters, numbers, special characters like symbols and spaces.  Strings have no limit on how long they may be and you may also take a string that contains no characters this is called a “empty string”.


What is the Python String Syntax?


The syntax of a string in Python is quite simple. We create a string by enclosing characters in quotes which can be single quotes, double quotes or triple quotes.  Which method you use is entirely up to you. Through out the tutorials I will be using single quotes for most of the tutorials except when certain situations arise where single quotes are not the best option.


Python String Syntax


‘This is a string in Python’


“This is a string in Python”


”’This is a string in Python”’


“””This is a string in Python”””


We can not start with single quote and end with a double quote.  We must start and end with same type of quote.


String Syntax Examples


We will be using a print statement in these examples we have not discussed the print statement.  The print statement tells Python to print the content contained in the print statement to the screen.  The syntax for the print statement is print().


#String Syntax Examples

>>> print('This is a string')
This is a string

>>> print("This is a string")
This is a string

>>> print('''This is a string''')
This is a string

>>> print("""This is a string""")
This is a string

 


Why Is A String a Sequence?


We mentioned earlier that a string is a sequence of data.  The data in a string takes on a positional ordering which means each character in a string has an exact position.  We can access the exact position of a character in a string by its index which is a numbered position of each character.  Each string index starts with the number 0 and counts up from that point(left to right).  Look at the example below.


 































This
is
a
String
0123456789101112131415

 


In the above example you can see that each letter and space have specific index position starting a 0. It is safe to say each character is its on data which is packaged together to create a string.  We will dive into this some more in chapter 4.


Strings are Immutable


Strings can not be changed once they have been created.  This is why they are considered immutable.  We can change a string by creating a new string but the current string can not be changed.


Python String Basics Conclusion


In this Python tutorial we took a very brief look at the Python string.  We didn’t even scratch the surface when it comes to strings but we can now create a string and we understand that each character in a string is in a specific order because strings are just a collection of letters, numbers, spaces and special characters.  We also learned that strings are immutable meaning once we create them we can not change them unless we create a new string.



No comments:

Post a Comment