Home » Tutorials » Python » Strings

Strings

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
strin

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.



Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "Strings". After Hours Programming. Accessed on October 25, 2024. https://www.afterhoursprogramming.com/tutorial/python/strings/.

  • Stewart, Suzy. "Strings". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/python/strings/. Accessed 25 October, 2024.

  • Stewart, Suzy. Strings. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/python/strings/.



38 thoughts on “Strings”

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

  2. 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 🙂

  3. 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

  4. 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.

  5. 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’)

  6. 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

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

Leave a Comment

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