Thursday, October 29, 2015

Python Tutorial: Iterate Through List Using For Loop in Python



Iterating Through List In Python read more at our website http://learnpythontutorial.com/iterating-through-list-in-python/



In this Python instructional exercise, we are going to figure out how to iterate through a Python list. Iterate means looping through a procedure in programming. We will figure out how experience a list and print every item contained in a list in Python. We will utilize a for loop in Python. We quickly took a gander at for loops in the string segment of our instructional exercises. The for loop will experience every article that shows up in a list and print that question or give back that protest one time. On the off chance that we have to give back every item contained in a list of the for loop is our best alternative. We could likewise fulfill this errand by indexing every article, except that would take forever and take a great deal of code. Our objective when programing is to restrain the measure of code composed to perform an assignment.



For Loop Syntax



for variable in arrangement:



print(variable)



For Loop Syntax Explained



for - Indicates to Python that we need to iterate through or loop through our sequence(list, dictionary(keys), tuples, strings, and so on.)



variable - This variable can be anything that meets the Python variable rules. I think about this variable as only a placeholder for every item contained in a list.



in - In is an administrator in Python that check for the enrollment. I think about this as is it in that list, word reference, tuple, string, and so on. This implies is it a player in that question.



: - Colon demonstrates the begin of a piece of code. We will see a considerable measure of colons later on instructional exercises



print() - Print is still some portion of the for loop which is alluded to as a square of code. The print articulation will print every item contained in the arrangement to us.



variable - This is the same variable in the for loop line this equitable calls the placeholder variable so Python knows which questions print.



Cases Of Iterating in Python Using The For Loop



 a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]



 for var in a:



... print(var)



...



1



2



3



4



5



6



7



8



9



10



a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] - We make a list protest that contains the numbers 1 through 10. We allocate the variable of "a" to speak to the list object.



for var in a: - We then perform a for loop on our list object. In the first place we incorporate the for which demonstrates we need to iterate through the list object. We then appoint every article contained in the list question a provisional variable which we allude to as var in this case. At that point we utilize the in administrator to advise our project to look in the list item speak to by the variable 'a'.



print(var) - Important we have to include two spaces before this line of code. We incorporate our print explanation to show we might want to print every item that is spoken to by the transitory variable of var.



Given back our articles - We are given back every one of items contained in our list object. Notice every one is imprinted on another line.



Essential Infomation About The For Loop



The principal line must have a colon( : ) toward the end.



The second line must be indented( two spaces)



We can utilize else discretionary statement in a for loop.



The interim variable can be anything you need the length of it meets Pythons variable rules.



Conclusion



In this Python instructional exercise, we figure out how to iterate through a list utilizing the Python for loop. On the off chance that you have any inquiries concerning iterating please leave a remark beneath.

Python Tutorial: Concatenating and Repeating List In Python #85





In this instructional exercise, we find out about concatenating and repeating list in Python. See our site for more data at http://learnpythontutorial.com/concatenating-and-repeating-list/



Concatenating and Repeating List



Concatenating and Repeating List



In this Python instructional exercise, we will concentrate on Concatenating and Repeating List in Python which will offer us some assistance with speeding up time we spend programming. On the off chance that we can join the list together which is call concatenation or repeat list we won't have to compose these list out. Concatenation and repeating list is the very same we did with strings. We utilize the in addition to image to link list and we utilize the reference bullets to increase the list in Python.



Concatenating List In Python



We can join two or more lists together utilizing concatenation as a part of Python. We connect utilizing the in addition to (+) between two or more lists. The concatenation of the list will give back another list object. To better comprehend concatenation in Python investigate our samples underneath.



Samples of List Concatenation In Python



>>> a = [1, 2, 3, 4, 5]



>>> b = [6, 7, 8, 9, 10]



>>> a + b



[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]



a = [1, 2, 3, 4, 5] - In this sample we make a list protest that is spoken to by the variable 'a'.



b = [6, 7, 8, 9, 10] - We likewise make another list protest yet this one will be spoken to by the variable 'b'.



a + b - We call both our list one is spoken to by the variable of "an" and the other spoke to by 'b'. We then utilize the concatenation administrator to train Python to consolidate the two list together.



[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] - We are given back another list protest that contains our two list which were consolidated utilizing list concatenation.



Repeating List in Python



We can repeat list utilizing the augmentation image which is known as the reference bullets. When we repeat list we are given back another list question that contains the repeated list objects in one list object.



Cases Of Repeating List in Python



>>> a = [1, 2, 3, 4, 5]



>>> a * 2



[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]



a = [1, 2, 3, 4, 5] - We make another list item and we utilize the variable of "a" to speak to our list object.



a * 2 - We take our list protest and utilize the repeat administrator which is alluded to as a reference marks. We then incorporate the number 2 to show that we need to repeat the list twice.



[1, 2, 3, 4, 5, 1, 2, 3, 4, 5] - We are given back a list item which has been repeated twice.



Conclusion



In this Python instructional exercise, we have taken a gander at concatenating list in Python and repeating list in Python. In the event that you have any inquiries concerning concatenating and repeating list in Python, please leave a remark underneath.



Python 3.5.0 was used in this instructional exercise.

Python Tutorial: Slicing List In Python - Python List



To Learn More about Slicing List In Python http://learnpythontutorial.com/slicing-list-in-python/



Slicing List In Python



In this Python instructional activity, we make sense of how to use slicing list in Python to get to various articles in a plan which are contained in a list. We can get to different things in a list using record, then again we would need to fulfill more work to do this so we can swing to slicing to grab challenges that are in a solicitation. Slicing list is similar to slicing strings.



Slicing List Syntax



[LIST][Start : Stop : Step]



[LIST] - This our list object which we will slice.



Start - This is the spot our slice will start. We use the list zone to start the slice of articles.



Stop - This is the spot our slice will stop. We use the list zone to stop the slice of articles. The record position used as a piece of the stop document truly demonstrates the stopping point which is one thing after the certifiable ending point.



Step - Step appears if the slice will skirt objects in a progression. Case in point, if we indicated 2 here then Python would skirt one another article.



Outlines Of Slicing List In Python



Outline 1



a = ['String', 12345, 'StR']



a[:]



['String', 12345, 'StR']



a = ['String', 12345, 'StR'] - We make a list question that contains two strings and one set numbers. We use the variable "a" to address the list object.



a[:] - We call the list objects by method for the variable 'a'. We then perform a slice from the start of the list to the end of the list objects. If we prohibit a starting list than the starting record will default to the 0 document position. In case we don't give a consummation record position then the conclusion list position will default to len(a) which will give back the amount of articles contained in the list object.



['String', 12345, 'StR'] - We are given back the full list since we sliced from the most punctual beginning stage to the end of the list.



Case 2



a = ['String', 12345, 'StR']



a[1:]



[12345, 'StR']



a = ['String', 12345, 'StR'] - We make a list dissent that contains three things. We use the "a" variable to identify with our list object.



a[1:] - We call our list object by method for the variable 'a'. We then slice from the 1 list position to the end of the string.



[12345, 'StR'] - We are given back a list that contains two things. We are given back a list starting at the 1 list position since we exhorted the slice to start at the 1 document position.



Test 3



a = ['String', 12345, 'StR']



a[0:2]



['String', 12345]



a = ['String', 12345, 'StR'] - We make a list question that contains three dissents and name the variable "a" to identify with our list object.



a[0:2] - We call the list object by method for the variable "an" and we slice from 0 record position to the number 2 document position. Remember while using the stopping record position the position before our stop position will be appeared.



['String', 12345] - We are given back a list question that contains the introductory two things in our list.



Test 4



a = [1, 2, 3, 4, 5]



a[0:6:2]



[1, 3, 5]



a = [1, 2, 3, 4, 5] - We make another list address that contains a couple numbers and is identified with by the variable 'a'.



a[0:6:2] - We call the list object through the variable "an" and after that we start our slice at the 0 list position and end our slice at the 6 record position. The third dispute is wandering and we are altering Python to give back one another number to us.



[1, 3, 5] - We are given back another list challenge that keeps away from one another number to return us 1, 3, 5



Conclusion



In this Python instructional activity, we look at slicing list in Python. In case you have any request concerning slicing list please leave a comment underneath.



Python 3.5.0 used as a piece of this instructional activity.

Wednesday, October 28, 2015

Python Tutorial: Indexing Python List - Python List #84



To learn more about list in Python visit our website at http://learnpythontutorial.com/indexing-list-in-python/

Indexing List In Python

In this Python tutorial, we will teach you all about indexing list in Python. Indexing list in Python is a very important concept which gives us the ability to access our objects that appear in a list. List in Python can contain a lot of information that is important in order to run our programs and having a way to access the content within a list allows us to use the content as it is needed when our program runs. Indexing allows us to access one object and slicing allows us to access numerous objects at one time.



Indexing List In Python Explained

[table id=9 /]



In the above table, you can see the indexing of a list is very similar to indexing strings. The only difference is that in the list each object holds an index position where in strings each character holds an index position. As always the index always starts at 0 and counts up for each object contained in a list. If we want to access an object going from the end of a list(right to left), we use a negative index number. The last index position when going right to left always starts at -1.



Examples Of Indexing List in Python

Access Index From Left To Right



 a = ['List', 12345, [123, 456]]

 a[1]

12345

a = ['List', 12345, [123, 456]] - We create a list object that contains a string object, number object and another list object. We assign our list object a variable named 'a' to represent the list.



a[1] - We call our list object via the variable 'a' then we request the index position of 1.



12345 - We are returned the 1 index positions object which happens to 12345.



Access Index From Right To Left



 a = ['List', 12345, [123, 456]]

 a[-1]

[123, 456]

a = ['List', 12345, [123, 456]] - We create a list object and assign the list a variable of 'a'.



a[-1] - We then call our list via the variable of 'a' and we then index from the right using a negative index position. Remember when indexing from the right we need to use negative numbers and the starting index position from the right is -1.



[123, 456] - We are returned a list that was contained in our list object. The list object is the last object contained in the list and we used -1 to access this list object.



Conclusion



In this Python tutorial, we looked at accessing list using indexing which is vital in programming when using list. If we can not access our content stored in list then list would be useless. We can pull one object out of a list using indexing if we need to pull more we could index multiple times or we can use slicing which we will cover in the next tutorial. If you have any questions about indexing in Python leave a comment below.



In this tutorial we use Python 3.5.0

Tuesday, October 27, 2015

Python Tutorial: Get The Length Of A List - Python List #83

Python Tutorial: Extend List Function With Range Function - Python List #82



Find The Length Of A List In Python

Find The Length Of A List In Python

In this Python tutorial we will take a look at how to find the length of a list in Python. Finding the length of a list in Python is very similar to finding the length of a string in Python. We simply use the Python len built-in function on our list and we will be returned the amount of objects contained in the list. Len returns the amount of objects in a integer format.

Len Built-in Function On A List Syntax

len(list argument)

  • len() - The len built-in function will return a integer to us indicating how many objects are contained in a list.
  • list argument - This is where we place our list argument which can be a list or variable that represents a list or the list built in function.

Examples Of Len Built-in Function On List

Example of Len Function On List

>>> len([1,2,3,4,5])
5
  • len([1,2,3,4,5]) - We call the len built-in function on our list object. Our list object contains five other integer objects.
  • 5 - We are returned an integer of 5 which means there is 5 objects in our list.

Example of Len Function On List Variable

>>> a = ['Hello', 'World']
>>> len(a)
2
  • a = ['Hello', 'World'] - We create a new list object that contains two string objects. We assign 'a' to represent the list object.
  • len(a) - We call the length built-in function on our list via the variable 'a'.
  • - We are returned 2 which indicates that we have two objects contained in our list.

Conclusion

In this tutorial we have learned how to find the length of a list in Python. If you have any questions about the length built-in function and list leave a comment below.

Wednesday, October 21, 2015

Python Tutorial: Everything You Need To Know About Strings In Python 3 -...

Python Tutorial: Advanced String Formatting In Python - Python Strings #77



Advanced String Formatting In Python

Advanced String Formatting In Python

In this Python tutorial, we will dig deeper into the Python formatting string method. We can perform a lot of tasks using the Python formatting string method in this tutorial, we will cover some of the most used features of this method but we cannot cover them all. If you would like to see the full list of features visits the Python documentation.
In the previous tutorial, we went over the basics of the Python formatting string method which gave us a good understanding of the power of string formatting in Python. When using formatting we can modify existing strings to help our programs to be more flexible and allows us to adapt our strings to fit certain situations.

Positions Content Using String Formatting in Python

We can use string formatting to position our content within a string just like the ljust string method, center string method and rjust string method. Let's Take a look at this in action.

Left Justify String Formatting Method

>>> '{:<40}'.format('Left')
'Left                                    '
Left Justify String Formatting Method Explained
  • '{:<40}'.format('Left') - In this example we add our placeholder({}) inside the curly brackets we insert colon(:) which is part of the string formatting syntax and then we add a carrot(<) pointing to the left which indicates we want to left justify the content. We also include 40 which is how many index spaces we want our new string object to span. The we call the format string method on our string object and provide a string as the argument.
  • 'Left                                    ' - We are returned a new string object that contains our argument and it is left justified with the string object expanding forty index positions.

Right Justify String Formatting Method

>>> '{:>40}'.format('right')
'                                   right'
  • '{:>40}'.format('right') We create a new string object that contains our placeholder({}) inside the placeholder we place a colon(:) which is part of string formatting syntax and we place a carrot(>) which is pointing to the right which indicates we want to right justify the content. We include 40 which states we want the new line object to span forty index positions.
  • '                                   right' - We are returned a new string object that contains our argument which is right justified within 40 index spaces.

Center Justify String Formatting Method

>>> '{:^40}'.format('center')
'                 center                 '
  • '{:^40}'.format('center') - We create a new string object which contains a placeholder({}) and this placeholder contains a colon which is part of the syntax, an up carrot(^) which indicates we want to center our content in the string object. We indicate we want our string to span forty index positions.
  • '                 center                 - We are returned a new string object which contains our argument which is centered within our forty index positions.

Binary String Formatting Method

If we would like to display the binary number of a value we can use the binary string format.

How To Get The Binary Number

>>> '{:b}'.format(12331)
'11000000101011'
  • '{:b}'.format(12331) We establish a new string object in which we will use to get the binary number of the value. We place our placeholder({}) in the string object. Inside our placeholder, we place the colon(:) which is part of the syntax then we insert a b which will indicate we want the binary representation of our value. Then we call format string method on our string object with the argument of 12331 and this is value we want to know the binary representation of our value.
  • '11000000101011' - We are our returned a new string object that contains our binary representation of the value 12331.

How To Get The Prefix For A Binary Number

>>> '{:#b}'.format(12331)
'0b11000000101011'
  • '{:#b}'.format(12331) - In the above example we get the binary number of our value in this example we include a # sign to indicate we want the prefix for a binary number. The rest remains the same.
  • '0b11000000101011' - We are returned a new string object that contains the binary representation of 12331 but we are also returned the the prefix for binary format which is 'Ob'.

Thousands Separator Via The Formatting String Method

>>> '{:,}'.format(21324232342342325)
'21,324,232,342,342,325'
  • '{:,}'.format(21324232342342325) - In this example, we want to separate our number into thousands to easy read the number. We achieve this by creating a new string object and we place our placeholder({}) in our string object. Then we place a colon(:) in our string object and then place a comma(,) in our placeholder. The comma indicates to Python that we want to comma separate our number. We then call the format string method on our string object and provide our value as an argument.
  • '21,324,232,342,342,325' - We are returned a string object that contains thousand separated string. Notice that this will place a comma in after every third number from the right.

Percentages In The Formatting String Method

>>> correct = 35
>>> total = 40
>>> 'Your Score Is: {:.1%}'.format(correct/total)
'Your Score Is: 87.5%'
  • correct = 35 - We create a new number object that is represented by the name correct.
  • total = 40 - We create a new number object that is represented by the name total.
  • 'Your Score Is: {:.1%}'.format(correct/total) - We establish a new string object that contains some content. We then place a placeholder({}) inside the placeholder we place a colon(:) which is a part of the syntax. Then we place a period(.) which indicates we want a decimal number returned. The number following the period indicates how many numbers after the decimal we would like to display. We then add the percentage(%) symbol to be contained in the returned string object. Then we call the format string method on our string object. We provide two arguments which are divided by each other.
  • 'Your Score Is: 87.5%' - We are returned a new string object that takes our arguments that have been divided by each other. 
     

Conclusion

In this Python tutorial we have looked at advanced string formatting string method. If you have any questions please leave a comment below.

Friday, October 16, 2015

Python Tutorial: Python String Formatting Method - Python Strings #76



To Learn More Visit our website at http://learnpythontutorial.com/python-string-formatting-method/



Python String Formatting Method

Python String Formatting Method

In the previous Python tutorial, we looked at the old way to format strings in Python in this tutorial we will explore the new preferred Python string formatting method. The Python string formatting method gives us better control over how we can modify string objects. The old way has some limitations to how string objects can be modified.



When formatting a string in Python using the format method we use curly brackets({}) for placeholders versus the old way where we would use the percentage or modulo symbol(%). If you need to use curly brackets in your string itself and you do not want it to be a placeholder then we can utilize two curly brackets to escape the brackets.



Python String Formatting Method Syntax

'String Object {placeholder}.format(arguments)



'String Object {placeholder}' - This our string object that will be modified by the format string method.  The {placeholder} is where we are able to insert content into the string object to create a new string object that contains the new content.

.format(arguments) - The format string method allows us to modify string objects to create a new string object with some inserted content.  The arguments are the content that is going to replace the placeholders in the original string object.

Examples of The Python String Formatting Method

Format String Method - Example 1

#Example 1

 'This is a {} in Python'.format('string')

'This is a string in Python'

Example 1 Explained



'This is a {} in Python'.format('string') - In this example, we set up a string object that contains a placeholder({}). The placeholder is where we are about place our argument. The format string method allows us to insert the content(argument) into our string object which will return us a new string object. The 'string' is our argument which will replace the curly brackets.



'This is a string in Python' - We are returned a new string object that contains our argument in the same position that our placeholder was in the previous string object.



Format String Method - Example 2

 'This is our {} in {}'.format('string', 'Python')

'This is our string in Python'

Example 2 Explained



'This is our {} in {}'.format('string', 'Python') - In this example, we create a new string object that contains two placeholders which will be replaced by our arguments which are contained in the right side of the format string method. When adding more than two arguments we must separate them with a comma. In this case when we run this line of code Python's format string method will take the first argument and place in the first placeholder position and then the second argument and place it in the second placeholder position.



'This is our string in Python' - We are returned a new string object with the first and second place holders replaced by the first and second arguments.



Format String Method - Example 3

 'This is our {1} in {0}'.format('Python', 'string')

'This is our string in Python'

Example 3 Explained



'This is our {1} in {0}'.format('Python', 'string') - In this example, we create a new string object which is similar to example 2 but notice we place a number 1 in the first place holder and a number 0 in the second placeholder.  We also change the order of the arguments in the format string method.  When this line is ran Python will look at the number contained in the placeholders which is actually index position of the arguments. It will then locate the argument via the index position and insert it into the new string object.



'This is our string in Python' - As you can see the format string method found the first index position which is our second argument and inserted that in our new string object. Then the format string method found our next argument at the 0 index position.



Format String Method - Example 4

'We are learning {language} {version}'.format(language='Python', version='3')

'We are learning Python 3'

Example 4 Explained



'We are learning {language} {version}'.format(language='Python', version='3') - We create a string object that contains our placeholders which are now represented by curly brackets and names. These names will be used to match the arguments with your place holders.

'We are learning Python 3' - We are returned a new string object that replaces the placeholders with the appropriate arguments.

Format String Method - Example 5

 language = ('Python', '3')

'We are learning {0[0]} {0[1]}'.format(language)

'We are learning Python 3'

language = ('Python', '3') - We create a tuple that contains two values. Then we assign a variable to represent the tuple.

'We are learning {0[0]} {0[1]}'.format(language) - We create a new string object and provide two placeholders. First number indicates the index position of the argument in the format string method.

Wednesday, October 14, 2015

Python Tutorial: Python String Formatting The Old Way - Python Strings #75



Learn More Visit our site at http://learnpythontutorial.com/python-string-formatting-the-old-way/



Python String Formatting The Old Way

In this Python tutorial we are going to talk about Python string formatting the old way. There are two ways to format a string in Python.  The old way of using a modulo which is sometimes referred to the printf style of formatting.  We focus on the printf style in this tutorial.  Python has mentioned several times over the last several years that they would remove this method of formatting strings. This method is yet to be yanked do to the fact that so many programs still use this method. The preferred method to formatting strings in Python is to the format string method. We will discuss the format string method in the next tutorial.



Formatting a string in Python allows us to add content to existing string object which will return us a new string object. We use string formatting often to modify content that will be displayed to a user. For example, we would like to take a username and say 'Hello, Username' we can take a string object and modify the string object to display 'Hello, John Doe' with some simple string formatting.



Python String Formatting The Old Way Syntax

'String Object %s' % 'Value'



'String Object %s' - This our string object that will be changed. %s represents the placeholder for a string object value.

% - This is our operator.

'Value' - The value is content that will replace the placeholder in a string object. If we are replacing one placeholder it can be in a string format if we are replacing more than one placeholder the value must be contained in a tuple or dictionary.

Examples Of Python String Formatting The Old Way

#Example 1

 'This is a %s' % 'String'

'This is a String'



#Example 2

 a = 'Hello, %s'

 userName = 'Johnny'

 a % userName

'Hello, Johnny'



#Example 3

 'We are learning %s %s' % ('Python', '3')

'We are learning Python 3'



#Example 4

 'We are learning %(language)s %(version)s' % {'language' : 'Python', 'version' : '3'}

'We are learning Python 3'

Examples Explained



Example 1:



'This is a %s' % 'String' - In this example we create a string object with a placeholder that will accept a string.  Then we place a value on the right side of the operator. The value will replace the placeholder. Since we only had one value to insert we can keep our value in a string format. We could still use the tuple or dictionary.



'This is a String' - We are returned a new string object that contains our value which replaced the placeholder.



Example 2:



a = 'Hello, %s' - We create a new string object that contains a placeholder for a string. The we give that string object a variable of 'a'.



userName = 'Johnny' - We create a new string object that holds our users name. We then assign the variable named 'userName' to the string object.



a % userName - We call our string object via the variable 'a' and then on the right side of operator we add a value via the variable 'userName'.



'Hello, Johnny' - We are returned a new string object that contains our first string object with the placeholder replaced by our value which was represented by 'userName'.



Example 3:



'We are learning %s %s' % ('Python', '3') - We create a new string object that contains two place holders for strings. Then on the right side of the operator we create a tuple to hold our two values that we will insert into our new string object. Remember that a tuple or dictionary must be used if there is more than one value to be inserted.



'We are learning Python 3' - We are returned a new string object that contains our new values



Example 4:



'We are learning %(language)s %(version)s' % {'language' : 'Python', 'version' : '3'} - We set up a string object that contains two place holders. These two place holders contain the keys from the dictionary. The keys are the values on the left hand side of the colon in a dictionary. The keys must be contained in parentheses and we still provide a type value on the outside of the parentheses. When the keys are called they will insert their values in place of the key.



'We are learning Python 3' - We are returned a new string object that contains the values in our dictionary which were called via their keys.



Conclusion



In this Python tutorial, we discussed Python string formatting the old way. If you have any questions about formatting Python strings leave a comment below and we will help you.

Wednesday, October 7, 2015

Python Tutorial: Python Upper String Method - Python Strings #74



Visit our website for more information http://learnpythontutorial.com/python-upper-string-method/

Python Upper string Method

Python Upper string Method

In this Python tutorial, we look at the Python upper string method which will convert all characters in the string object to uppercase characters. The upper string method takes no arguments.



Python Upper String Method Syntax

'String Object'.upper()



'String Object' - This is our string object which we are going to call upper string method on.

.upper() - The upper string method will convert all characters to uppercase in a new string object.

Examples Of The Python Upper String Method

#Example 1

 'This is a string'.upper()

'THIS IS A STRING'



#Example 2

 'this is another string'.upper()

'THIS IS ANOTHER STRING'



#Example 3

 a = 'new string'

 a = a.upper()

 a.isupper()

True

Examples Explained



Example 1:



'This is a string'.upper() - We create a new string object and call the upper string method on our string object.



'THIS IS A STRING' - We are returned a new string object that contains all uppercase characters.



Example 2:



'this is another string'.upper() - We create a new string object and call the upper string method on our string object.



'THIS IS ANOTHER STRING' - We are returned a string object that contains all uppercase characters.



Example 3:



a = 'new string' - We create a new string object and assign the variable a to the new string object.



a = a.upper() - We call the upper string method on our string object via the variable a then we reassign the variable a to the new string object



a.isupper() - We then call the isupper string method to show that characters have been converted to uppercase.



Conclusion



In this tutorial we took a look at the upper string method in Python. This string method will convert all characters to uppercase in a new string object. If you have any questions about the upper string method in Python leave a comment below.

Python Tutorial: Python Swapcase String Method - Python Strings #73





To Learn More Visit our Website: http://learnpythontutorial.com/python-swapcase-string-method/



Python Swapcase String Method

Python Swapcase String Method

In this Python tutorial, we look at the Python swapcase string method which allows us to be able to switch uppercase characters to lowercase and lowercase to uppercase. The swapcase string method is very straight forward if you have a lowercase character it will be converted to uppercase and uppercase will be converted to lowercase.  There are no arguments for this string method.



Python Swapcase String Method Syntax

'String Object'.swapcase()



'String Object' - This is our string object that we are going to call swapcase string method on.

.swapcase() - Swapcase string method will switch the case of a character. There is no arguments for the swapcase string method.

Examples Of The Swapcase String Method

#Example 1

 'this is a string'.swapcase()

'THIS IS A STRING'



#Example 2

 'THIS IS ANOTHER STRING IN UPPERCASE'.swapcase()

'this is another string in uppercase'

Examples Explained



Example 1:



'this is a string'.swapcase() - We create a new string object and call the swapcase string method on the string method.



'THIS IS A STRING' - We are returned a new string object that contains all uppercase characters.



Example 2:



'THIS IS ANOTHER STRING IN UPPERCASE'.swapcase() - We create a new string object and we then call the swapcase string method on our object.



'this is another string in uppercase' - We are returned a new string object that contains all lowercase characters.



Conclusion



In this Python tutorial we took a look at the swapcase string method which allows us to change the case of a string object. If you have any questions about the swapcase string method leave a comment below.

Tuesday, October 6, 2015

Python Tutorial: Python Strip String Method - Python String #72



For more information visit our website at http://learnpythontutorial.com/python-strip-string-method/



Python Strip String Method

Python Strip String Method

In this Python tutorial, we're going to focus on the Python strip string method. Python strip string method has the capability to strip content from the left or right side of the string object. Strip string method takes one argument the argument must be in a string format and if this argument is blank then it will move the white space from either left or right or both sides of the string object. The strip string method removes content from the left and right hand side of the string object where the left strip string method removes content from the left-hand side and the right strip string method removes from the right-hand side of a string object.



Python Strip String Method Syntax

'String Object'.strip('chars')



'String Object' - This is our string object which were going to call the python strip string method on.

.strip() -  The strip string method removes content from the left and right side of the string object. When the strip string method comes across the character that does not match the argument the strip string method will stop.

'Chars' - This argument is case-sensitive and does not have to be in any particular order. Python will take the argument and remove any characters that matched the string object and remove those characters and return a new string object.

Example Of The Python Strip String Method

#Example 1

 '    This is a string    '.strip()

'This is a string'



#Example 2

 'This is a string in Python'.strip('nThiosn')

' is a string in Pyt'



#Example 3

 'http://learnpythontutorial.com'.strip('htp://')

'learnpythontutorial.com'

Examples Explained



Example 1:



'    This is a string    '.strip() - In this example we create a string object and we called the strip string method on our string object. We provide no argument so the strip string method will remove only white space.



'This is a string' - We are return a string object says no whitespace on either the left or right inside.



Example 2:



'This is a string in Python'.strip('nThiosn') - In this example we create a new string object and we call the strip string method on our string object. We provide an argument that contains some characters that would like to move from the left and right hand side of the string object.



' is a string in Pyt' - We are returned a string object with some characters made from the left and right side of the string object.



Example 3:



'http://learnpythontutorial.com'.strip('htp://') - in this example we create string object and we give an argument to remove HTTP:// from our string object.



'learnpythontutorial.com' - We are returned a new string object that removes the 'http://' from the string object.



Conclusion



In this Python tutorial we look at the Python strip string method. If you have any questions please leave a comment below.

Python Tutorial: Python Startswith String Method - Python Strings #71



 Learn more at our website http://learnpythontutorial.com/python-startswith-string-method/



Python Startswith String Method

Python Startswith String Method

In this Python Tutorial, we will take a look at the Python startswith string method. The startswith string method allow us to check if a string object starts with certain characters. The startswith string method has one mandatory argument which must be in a string format this argument is matched the start of the string object. Unlike strip string methods, this argument must match the order and is also case-sensitive. If startswith string method finds a, match then we will be returned a boolean of True and if it does not match then we are returned a boolean of False. We also have two optional arguments which are the start index position and the end index position. The default start index position is the start of the string object and the default end position is the end of the string object.



Python Startswith String Method Syntax

'String Object'.startswith('substring', start, stop)



'String Object' - This is our string object which we are going to call the startswith string method on.

.startswith() - This string method will check if the string object starts with the startswith the substring argument and if it matches then we are returned True and if it does not match then we are returned False.

'substring' - This is the argument that we provide the string method. This argument will be searched through the string object if there is a match then we are returned True and if it does not match then we are returned False. The argument is case-sensitive and the order must also match.

start - The start argument will indicate where the startswith string method will start the search for an argument match. This argument defaults to the start of the string.

stop - The stop argument will indicate where the the search will end. The default value is the end of the string object.

Examples Of The Python Startswith String Method

Example 1:

 'This is a string'.startswith('This')

True



Example 2:

 'A new string'.startswith('new', 2)

True



Example 3:

 'My String'.startswith('string', 3, 9)

False

Examples Explained



Example 1:



'This is a string'.startswith('This') - in this example we created new string object and call the startswith string method on our string object.  we provide one argument to our string method ('This').



True -  We are return True because this startswith string method found 'This' in the beginning of the string object.

Example 2:



'A new string'.startswith('new', 2) - We create a new string object and then we call this startswith string method on the string object and then we provide two arguments first argument is 'new' and the second argument is the starting point for our search which is second index position.



True - We are returned True because the Python startswith string method located 'new' which starts at the Index position 2.

Example 3:



'My String'.startswith('string', 3, 9) - We create a new string object and then we call startswith string method on our string object. We provide three arguments first argument is 'first'. Second argument is index position 3 and the third argument is index position 9.



Conclusion



In this Python tutorial we take a look at the Python startswith string method. If you have any questions leave a comment below.

Python Tutorial: Python Rstrip String Method - Python Strings #70





For more information visit our website at http://learnpythontutorial.com/python-rstrip-string-method/



Python Rstrip String Method

Python Rstrip String Method

In this tutorial, we will look at the Python rstrip string method. The rstrip string method strips content from the right side of the of the string. The rstrip string method takes one argument which is referred to as the chars which are the characters we want to remove from the string. The argument must be in a string format, but these characters don't have to be in any specific order. When the rstrip string method crosses a character that is not contained in the argument rstrip will stop processing. The default argument will remove white spaces from the right side of the string object.



Python Rstrip String Method Syntax

'String Object'.rstrip('chars')



'String Object' - Our string object which we will call the rstrip string method on.

.rstrip() - The rstrip string method will remove content from the right side of the string method. The rstrip() will take the characters contained in the argument and remove those characters right to left. When rstrip comes across a character that is not contained in the argument than the rstrip method will stop.

'Chars' - This is the argument that we provide the rstrip string method. This method will search right to left and remove any characters from the string object that is contained in the argument. The argument is case-sensitive. If we leave this argument blank then the string method argument will default to white spaces and the string method will only remove white spaces from the right side of the string object.

Examples Of The Python Rstrip String Method

#Example 1

 '    Python    '.rstrip()

'    Python'



#Example 2

 'We are learning Python'.rstrip('nohtyP')

'We are learning '



#Example 3

 'CAPS'.rstrip('SpAc')

'CAP'

Examples Explained



Example 1:



'    Python    '.rstrip() - In this example, we create a string object that contains spaces on the right and left side. We then call the rstrip string method on our string object. We provide the string method with a blank argument which will default to remove white spaces on the right side of the of the string object.



'    Python' - We are returned a new string object that contains no white spaces on the right of the string object.



Example 2:



'We are learning Python'.rstrip('nohtyP') - In this example, we create a new string object and then call the rstrip string method on the string object. We provide the string method with an argument of ('nohtyP') which is Python spelled backwards. This example shows that it does not have to be in a specific order. Note that we use uppercase 'P' since the rstrip string method is case-sensitive.



'We are learning ' - We are returned a new string object that has 'Python' removed.  Notice we did not remove the whitespace since there was no whitespace in the argument.



Example 3:



'CAPS'.rstrip('SpAc') - In this example we create a new string object and call the rstrip string method on our string object. This example, proves that rstrip is case senstive.



'CAP' - Notice that we only removed the 'S' from the string object. This was done because the argument is case-sensitive.



Conclusion



In this Python tutorial, we took a look at the Python rstrip string method if you have any questions about this string method leave a comment below.

Friday, October 2, 2015

Python tutorial: Python Rindex String Method - Python Strings #68



To learn more about the Python Rindex String Method visit our website at http://learnpythontutorial.com/python-rindex-string-method/

Python Rindex String Method

In this Python tutorial, we will explore the Python rindex string method. The rindex string method is similar to the rfind string method the only difference is the rindex method returns a value error when a substring is not found in the string object and the rfind returns -1 when the substring is not located.



The rindex string method takes three arguments. The first argument is mandatory and that is the substring. The substring is the string we are about search for in the string object. If the substring is located in the string object, then we are returned the index position where the substring was located in the string object and if the substring is not located we are returned a value error. The next two arguments are voluntary. The second argument is the start index position and if do not provide a starting point then the rindex will default to the start of the string. The third argument is stopping index position and the default value is the end of the string object.



Python Rindex String Method Syntax

'String Object'.rindex('substring', start index, stop index)



'String Object' - The string object is where our rindex string method is going to search for a match.

.rindex() - The index string method searches through a string object to find the last occurrence in a string object and returns the index position of the last occurrence.

'substring' - The substring is what rindex is going try to match in the string object. If the substring is found in the object then Python will return the last occurrences index position. This argument is required and must be in string format.

start index - The start index is where the rindex will start its search. This is an optional argument and the default value is the beginning of the string.

stop index - The stop index is an optional argument. The stop index will indicate to the rindex string method where to stop the search for a match in the string object.  If the stop index is being used then there must be a start index position.

Examples Of The Python Rindex String Method

#Example 1

 'We are learning Python'.rindex('ing')

12



#Example 2

 'This is a string in Python'.rindex('n', 10)

25



#Example 3

 'This is a string in Python'.rindex('n', 10, 20)

18



#Example 4

 'This is a string in Python'.rindex('x', 10, 20)

Traceback (most recent call last):

  File "stdin", line 1, in module

ValueError: substring not found

Examples Explained



Example 1:



'We are learning Python'.rindex('ing') - In this example we create a string object and then call the rindex string method on the string object. We provide the rindex string method with one required argument. The substring argument is 'ing'.



12 - We are returned 12 this is the last occurrence in the string object.

Example 2:



'This is a string in Python'.rindex('n', 10) - In this example we create a new string object and call the rindex string method on the string object. We provide a substring argument of 'n' and a starting index position of 10.



25 - We are returned 25 this is position of the last occurrence in the string object

Example 3:



'This is a string in Python'.rindex('n', 10, 20) - We create a new string object and call the rindex string method on our string object. We provide a substring argument of 'n', a starting argument of 10 and ending argument of 20.



18 - We are returned 18 this is the last occurrence in the search position we provided.

Example 4:



'This is a string in Python'.rindex('x', 10, 20) - We create a new string and call our string method of rindex on our string object. We provide a substring argument of 'x', a starting argument of 10 and a ending argument of 20.



Traceback (most recent call last):



  File "stdin", line 1, in module



ValueError: substring not found - We are returned a value error since the 'x' substring was not located in the string object.



Conclusion



In this Python tutorial, we took a look at the Python rindex string method. If you have any questions about the rindex string method leave a comment below.

Python Tutorial: Python Rfind String Method - Python Strings #67





For more information on the Python Rfind String Method visit our website at http://learnpythontutorial.com/python-rfind-string-method/

Python Rfind String Method

In this Python tutorial, we will learn how to use the Python rfind string method. The rfind string method will find the highest occurrence of the substring argument which is the last match in the string object. The rfind string method searches a string from the left to right to find the substring argument and return an index position of the match. The rfind string method takes three arguments. The first argument is our substring which is what Python is about search for in the string object. The second argument which is optional and indicates the starting position for the search and default position is the start of the string object. The third argument which is also optional and indicates the ending position of the search and the default value is the end of the string. If you want to declare an ending position you must declare a starting position argument. If Python does not find a match for the substring in the string object, then Python will return a -1.



Python Rfind String Method Syntax

'String Object'.rfind('substring', start, stop)



'String Object' - The string object is the string that we be searched using the rfind string method.

.rfind() - The rfind string method will search the string object for our substring and return the highest index position match.

'substring' - The substring is the string that will be searched for in the string object. If the substring is found in the string object then Python will return a index position of the last match. The substring search is case sensitive.

start - The start position is where the rfind string method will start the search for the substring. If no search position is given then the start position will default to the beginning of the string object.

stop - The stop position is where the rfind string method will stop the search for the substring. Remember when slicing the ending index ends one position before the indicated position.

Examples Of The Python Rfind String Method

#Example 1

 'This is our string'.rfind('st')

12



#Example 2

 'This is our string'.rfind('is', 4)

5



#Example 3

 'This is our string'.rfind('i')

15



#Example 4

 'This is our string'.rfind('i', 4, 7)

5



#Example 5

 'This is our string'.rfind('t', 4, 7)

-1

Examples Explained



Example 1:



'This is our string'.rfind('st') - We create a string object and call the rfind string method on our object. Our string method has an substring argument which the rfind method will search for in our string object.



12 - We are returned 12 which is the index position and is our last occurrence of our substring ('st')



Example 2:



'This is our string'.rfind('is', 4) - We create a new string object and call the rfind string method on the string object. Our method takes an argument of ('is') for the substring and 4 for the index position to start the search.



5 - We are returned 5 since the last occurrence in our string object is at the 5 index position.



Example 3:



'This is our string'.rfind('i') - We create a new string object and then we call the rfind string method on our string object. We provide one argument for the substring of ('i').



15 - We are returned 15 since the rfind string method finds the last 'i' in the string object.

Example 4:



'This is our string'.rfind('i', 4, 7) - We create a new string object and call the rfind string method on our string object. We provide a substring argument of 'i' and we also provide a start argument of 4 and stop argument of 7.



5 - We are returned 5 since the rfind string method on searches between the 5 index position and the 7 index position but remember the stopping point is actually the 6 index position.

Example 5:



'This is our string'.rfind('t', 4, 7) - We create a new string object and call the rfind string method on our string object. We provide an argument of 't' for the substring which we be searched for in the string object. We then provide a starting index position of 4 and an ending position of 7 which is actually 6.



-1 - We are returned a -1 since Python did not find a 't' between the index positions of 4 and 7.

Conclusion



In this Python tutorial we looked at the Python Rfind String Method. If you have any questions about the rfind string method leave a comment below and we will do our best to help you out.

Thursday, October 1, 2015

Python Tutorial: Python Replace String Method #66



See our full tutorial on the Python Replace String Method at http://learnpythontutorial.com/python-replace-string-method/

Python Replace String Method

In this Python tutorial we are going to look at the Python replace string method. The python replace string method gives the ability to replace content in an existing string and create a new string object with the replaced content. The replace string method takes three arguments. First argument is the content that will be replaced and must be in a string format. The second argument is the content that will replace the existing content this argument must be in a string format as well. The third argument is how many occurrences will be replaced.



Python Replace String Method Syntax

'String Object'.replace('string content', 'replace content', number of occurrences)



'String Object' - Our string object where we are going to replace content.

.replace() - This string method replaces content in an existing string and creates a new string object.

'string content' -  The content in the existing string object that will be replaced. Must be in a string format.

'replace content' - This is the content that replace the content in an existing string object. Must be in a string format.

number of occurrences - How many occurrences would you like to be replaced. Default is replace all occurrences.

Examples Of The Python Replace String Method

#Example 1

 'This is a string'.replace('is', 'IS')

'ThIS IS a string'



#Example 2

 'This is a string'.replace('is', 'IS', 1)

'ThIS is a string'



#Example 3

 'Mississippi'.replace('i', 'I')

'MIssIssIppI'

Examples Explained



Example 1:



'This is a string'.replace('is', 'IS') -We create a string object and then call the replace string method on our string object. Our string method has two arguments. The first argument is the content we want to replace and the second argument is content we want to insert into our string object. We did not provide an occurrence argument on this string method.



'ThIS IS a string' - We are returned a string where two parts of our content that was replaced by with uppercase 'IS'.



Example 2:



'This is a string'.replace('is', 'IS', 1) - We create a string object and call the replace string method on our string object. We provide our replace string method with 3 arguments. First argument of 'is' is going to be replace with the second argument of 'IS' and then our third argument says only replace the first occurrence.



'ThIS is a string' - We are returned a new string object that contains the newly replaced part of the string.



Example 3:



'Mississippi'.replace('i', 'I') - We create a new string object and call the replace string method on the object.



'MIssIssIppI' - We are returned a new object where the lowercase 'i' is replaced with an uppercase 'I'.



Conclusion



In this tutorial we look at the Python replace string method which gives us the ability to replace parts of a string object and create a new object. If you have any questions please leave a comment below.

Python Tutorial: Python Lstrip String Method #65





T0 learn more about the Python Lstrip String Method visit our website http://learnpythontutorial.com/python-lstrip-string-method/



Python Lstrip String Method

In this Python tutorial, we will explore the Python lstrip string method. The lstrip string method allows us to remove content from the left side of a string. The lstrip string method takes one argument in a string format. This argument will remove any combination of the characters in the string object that is contained in the argument. If there is no argument provided to the lstrip string method, then the method will revert to a default white space.



Python Lstrip String Method Syntax

'String Object'.lstrip('characters to be removed')



'String Object' - This is our string object that we are going to remove content from the left side of the string.

.lstrip('characters to be removed') - The lstring string method has one optional argument. This argument will remove any combination characters in the argument from the left of the string object. The default argument is white space if we do not include an argument then Python will remove any spaces from the left side of the string.

Examples Of The Python Lstrip String Method

#Example 1

 '   This is a string   '.lstrip()

'This is a string   '



#Example 2

 'abcdef'.lstrip('cba')

'def'



#Example 3

 'abcdef'.lstrip('ABC')

'abcdef'



#Example 4

'http://www.learnpython.com'.lstrip('/:.wpth')

'learnpython.com'

Examples Explained



Example 1:



'   This is a string   '.lstrip() - We create a string object that contains spaces on the left and right side of the string object. Then we call the lstrip string method on the string object.



'This is a string   ' - We are returned a new string object with the spaces on the left removed since the default argument for the lstrip string method is spaces.



Example 2:



'abcdef'.lstrip('cba') - We create a new string object and call the lstrip string method on our string object. Our string method takes an argument of 'cba' so if any of these characters show up in our string object they will be removed.



'def' - We are returned 'def' since our arguments met the first three characters of our string object.



Example 3:



'abcdef'.lstrip('ABC') - We create a string object and call lstrip string method on our string object. We provide an argument of 'ABC'.



'abcdef' - We are returned the same string since the argument is case sensitive so it did not match any of the characters in the string object.



Example 4:



'http://www.learnpython.com'.lstrip('/:.wpth') - We create a string object that contains a web address and then we call the lstrip string method to remove the 'http://www.' from the web address.  We only need to add one character to the argument.



'learnpython.com' - We are returned a web address with the 'http://www.' removed.



Conclusion



In this Python tutorial we look at the Python lstrip string method.  This method allows us to remove content from the left side of a string. If you have any questions about the lstrip string method leave a comment below.