Skip to Content


You (Level 0)
0% Complete
Last Badge Earned

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!

Example myString = ""
print (type(myString))
Result <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.

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

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.

For a more tangible and better look into the Python language, consider reading the following book. It's an excellent read.

Your code will execute in this window.

Let's explore some more tutorials or topics!

Next Tutorial Previous Tutorial


If you enjoyed this resource, support me by sharing this page with others.

Comment on

Show Comments
  • User

    jvmjunior Jan. 19, 2017, 4 p.m.

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

  • User

    Rafa Aug. 19, 2015, 1:56 p.m.

    Lol im just 10 years old and made a game

  • User

    Arnie June 30, 2015, 4:59 p.m.

    myString = "" print (type(myString)) This gives nothing in the code simulator?

  • User

    Arnie June 30, 2015, 4:53 p.m.

    The test code function is not working?

  • User

    Alex Stevenson June 16, 2015, 11:37 a.m.

    What syntax links the stringVar... Code to what it's manipulating?

  • User

    Foro5 March 28, 2015, 6:34 a.m.

    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). ºººº

  • User

    BRIAN March 23, 2015, 2:41 p.m.

    mystring = "" print(type(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 :)

  • User

    dan March 17, 2015, 2:03 p.m.

    The are also not picky by the amount of characters you put in them. the should be they

  • User

    sng March 4, 2015, 8:25 a.m.

    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

  • User

    Omid Mohammadi Feb. 27, 2015, 12:29 p.m.

    To all that have problems, test this: a = "What a piece of work is a man" print (a.count('a'))

  • User

    Aksinia Feb. 24, 2015, 7:08 p.m.

    Code simulator is not going to execute the first example: myString = "" print (type(myString)) Works good in pyCharm 3.4.1.

  • User

    Hamilton Cook Feb. 22, 2015, 9:16 p.m.

    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 <class 'str'> 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.

  • User

    Guru the Wizard Feb. 13, 2015, 8:31 p.m.

    remember to use with already set string variable aakash

  • User

    aakash Jan. 10, 2015, 11:46 a.m.

    type() not working in simulator

  • User

    Shinwa Dec. 15, 2014, 11:38 a.m.

    stringVar = "What a piece of work is a man." stringVar.count('a')

  • User

    archangel Nov. 27, 2014, 5:26 a.m.

    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." stringVar.count('a')

  • User

    Kazaminese Nov. 23, 2014, 5:44 a.m.

    StringVar is the name of your string Like a= "mofo" a.count('o') The result would be 2

  • User

    JamaraJackson Nov. 21, 2014, 7:13 a.m.

    I am using a online python code ( and I am having trouble using stringVar command. Can I get assistance from someone?

  • User

    NoFawkesToGive Nov. 21, 2014, 2:15 a.m.

    how do you go through python shell?

  • User

    vingtsun01 Nov. 4, 2014, 6:54 p.m.

    a = "a..b..c..d..e..f..g..h" print (a[::-1]) print(a[::1].upper()) print (a[::-3]) print(a[::3].upper())

  • User

    vingtsun01 Nov. 4, 2014, 6:49 p.m.

    a = "ballsitch" print (a[1:4]) print (a[:-4]) print(a[5:].upper())

  • User

    ravenx May 19, 2014, 1:52 p.m.

    a = &quot;string&quot; print(a[1:3]) print(a[:-3])

  • User

    rousbel_villar March 9, 2014, 5:11 p.m.

    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.

  • User

    panda March 7, 2014, 8:28 p.m.

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

  • User

    panda March 7, 2014, 8:23 p.m.

    i've got nothing returned when input the first about you?

  • User

    Lotfi GHAZOUANI Feb. 26, 2014, 2:07 p.m.

    With the first example there is no output. It's more important to work with the Python shell.

  • User

    Qi2014 Feb. 16, 2014, 11:59 a.m.

    There is some problem the code myString = &quot;&quot; print (type(myString)) There was nothing in &quot;Output Section&quot;

  • User

    Madinawi Feb. 10, 2014, 5:15 p.m.

    a = &quot;string&quot; print (a[:4]) print (a[:])

  • User

    Zack Dubit Jan. 10, 2014, 12:13 p.m.

    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.

  • User

    Muhammad Usman Jan. 7, 2014, 2:27 a.m.

    Here is some problem with Test Code Panle. I write the code myString = &quot;&quot; print (type(myString)) There was nothing in &quot;Output Section&quot;, but the output was on browsers console

  • User

    NinthGate Nov. 13, 2013, 6 p.m.

    a = &quot;Quote the raven &#92;&quot;Nevermore&#92;&quot;&quot; print (a [-10:-1])

  • User

    Neelima Oct. 25, 2013, 10:44 p.m.

    My bad. I misread the instructions.:/

  • User

    Neelima Oct. 25, 2013, 10:37 p.m.

    I believe that the StringVar.....() methods are not correct way to write. I believe that is is better to take a look at for the string methods.

  • User

    SephraDeath Oct. 22, 2013, 8:20 a.m.

    I just wanted to let you know that the first example does not work not even if you put a string into the quotes.

  • User

    dheeraj Oct. 6, 2013, 9:49 p.m.

    very easy to learn

  • User

    Igor Mazurok Sept. 23, 2013, 12:42 p.m.

    What about 'Hi', &quot;Hi&quot;, '''Hi''' and &quot;&quot;&quot;Hi&quot;&quot;&quot;?

  • User

    Cacmo July 17, 2013, 11:33 p.m.

    Easy to learn

  • User

    sirius May 19, 2013, 3:41 p.m.

    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

  • User

    Sky Flying April 2, 2013, 4:52 a.m.

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