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!
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.
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.
Hi,
The simulator is not working when for the execution of the function type().
No error is shown but neither the result is.
Lol im just 10 years old and made a game
myString = “”
print (type(myString))
This gives nothing in the code simulator?
The test code function is not working?
What syntax links the stringVar… Code to what it’s manipulating?
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). ºººº
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 🙂
The are also not picky by the amount of characters you put in them. the should be they
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
To all that have problems,
test this:
a = “What a piece of work is a man”
print (a.count(‘a’))
Code simulator is not going to execute the first example:
myString = “”
print (type(myString))
Works good in pyCharm 3.4.1.
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.
remember to use with already set string variable aakash
type() not working in simulator
stringVar = “What a piece of work is a man.” stringVar.count(‘a’)
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’)
StringVar is the name of your string
Like
a= “mofo”
a.count(‘o’)
The result would be 2
I am using a online python code (http://labs.codecademy.com/#:workspace) and I am having trouble using stringVar command.
Can I get assistance from someone?
how do you go through python shell?
a = “a..b..c..d..e..f..g..h”
print (a[::-1])
print(a[::1].upper())
print (a[::-3])
print(a[::3].upper())
a = “ballsitch”
print (a[1:4])
print (a[:-4])
print(a[5:].upper())
a = "string"
print(a[1:3])
print(a[:-3])
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.
please ignore my last comment.As Lotfi GHAZOUANI commented,you’d better pattern the first example in Python shell
i’ve got nothing returned when input the first instance.how about you?
With the first example there is no output.
It’s more important to work with the Python shell.
There is some problem the code
myString = ""
print (type(myString))
There was nothing in "Output Section"
a = "string"
print (a[:4])
print (a[:])
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.
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
a = "Quote the raven \"Nevermore\""
print (a [-10:-1])
My bad. I misread the instructions.:/
I believe that the StringVar…..() methods are not correct way to write. I believe that is is better to take a look at
http://docs.python.org/2/library/stdtypes.html#string-methods for the string methods.
I just wanted to let you know that the first example does not work not even if you put a string into the quotes.
very easy to learn
What about ‘Hi’, "Hi", ”’Hi”’ and """Hi"""?
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
I am pleased with the fundamentals of Python is very excellent for beginner either beginner with no experience.. Bravo!!