As with any programming language, strings are one of the most important things to know about Python. Also as we have experienced in the other languages so far, strings contain characters. Strings are not picky by any means. They can contain almost anything if used properly. The are also not picky by the amount of characters you put in them. Quick Example!

myString = ""
print (type(myString))

<class ‘str’>

The type() is awesome. It returns the variable type of whatever is inside the parentheses, which is very useful if you have a few bugs that you can’t figure out or if you haven’t looked a large chunk of code for awhile and don’t know what type the variable is. Back to the amount of characters in a string, we can see even an empty set of “” returns as a string. Strings are powerful and are very easy to declare. Let’s look into some common string methods so you can get your hands dirty.

Common String Methods in Python

  • stringVar.count(‘x’) – counts the number of occurrences of ‘x’ in stringVar
  • stringVar.find(‘x’) – returns the position of character ‘x’
  • stringVar.lower() – returns the stringVar in lowercase (this is temporary)
  • stringVar.upper() – returns the stringVar in uppercase (this is temporary)
  • stringVar.replace(‘a’, ‘b’) – replaces all occurrences of a with b in the string
  • stringVar.strip() – removes leading/trailing white space from string

String Indexes

One of the really cool things about Python is that almost everything can be broken down by index and strings are no different. With strings, the index is actually the character. You can grab just one character or you can specify a range of characters.

a = "string"
print (a[1:3])
print (a[:-1])


Let’s discuss the print (a[1:3]) because it is the easiest to explain. Remember that Python starts all indexes from 0, which would have been the ‘s’ in our variable a. So, we print out ‘tr’ because we printed everything up to our range of 3, but not 3 itself. As for the second example, welcome to a beautiful part of Python. Essentially, specifying a negative number after a : in an index means that you want python to calculate the index starting from the end and moving toward the front aka backwards. So, we tell python that we want everything from the first character to the second to last character. Take a breather, you earned it.

If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Share on StumbleUpon
Digg this
Tweet about this on Twitter
Share on Reddit

Leave a Comment

  1. I am pleased with the fundamentals of Python is very excellent for beginner either beginner with no experience.. Bravo!!

  2. Thank you
    You have an ease of expression
    You can explain and introduce new notions simply
    and any one can understand what it is presented.
    I will be very interesting to see new sections in this wonderfull website,
    explaining the advanced parts of Python programming and Software Design too.
    Thank you very much again

  3. Here is some problem with Test Code Panle. I write the code
    myString = ""
    print (type(myString))

    There was nothing in "Output Section", but the output was on browsers console

  4. Both the type function and all of the methods do not seem to be working on the program. No error message appears but the expected response is not present either.

  5. please ignore my last comment.As Lotfi GHAZOUANI commented,you’d better pattern the first example in Python shell

  6. same thing hapenned to me panda 100 , i had to put the example on the shell i
    had installed , in order to run the code.

  7. Am I missing something?

    The commands with string.Var, listed above DO NOT WORK, e.g., stringVar.count(‘x’), etc.

    If they do work then what is wrong with this which doesn’t work?:

    stringVar = “What a piece of work is a man.”

  8. ok I typed in:

    myString= “”
    print (type(myString))

    and it resulted in a blank screen. Is this a mistake by the site?

    It said the result would be but i got a blank screen.

    also why does this same code not work with python idle? I keep getting error messages

    also why is print not moved 4 spaces over. is it a new rule? I am new to programming of any kind.

  9. Code simulator is not going to execute the first example:
    myString = “”
    print (type(myString))

    Works good in pyCharm 3.4.1.

  10. stringVar.replace(‘a’, ‘b’) – replaces all occurrences of a with b in the string, does not change the string

    stringVar.strip() – removes leading/trailing white space from string, does not change the string

  11. mystring = “”

    gives an error message on the simulator, when l tried using Python 3.4.2 IDE it worked fine. l think iys a bug on the simulator, just try on the IDE you will get results 🙂

  12. print (a[n:m]) ºººº
    Print first m characters of var a starting at character with index n. (Note: string characters are indexed from zero and m refers to characters count, not characters index) ºººº
    print (a[:-m]) ºººº
    Print all var a characters removing the last m, starting with index 0 (Note: since n was ommited, it is zero and since m is a count remove that number of characters at the end of the string). ºººº

  13. Hi,

    The simulator is not working when for the execution of the function type().
    No error is shown but neither the result is.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.