diff options
author | Amit Sethi | 2010-04-20 13:39:41 +0530 |
---|---|---|
committer | Amit Sethi | 2010-04-20 13:39:41 +0530 |
commit | 64851a82bed63a57240aa87a7cd0a83122cc1e75 (patch) | |
tree | c8a2eb50ba223f255d38727618e123d041d7e1ec /basic-python.txt | |
parent | 811891e9eee379ab5fc1c508970ec95870569a75 (diff) | |
download | st-scripts-64851a82bed63a57240aa87a7cd0a83122cc1e75.tar.gz st-scripts-64851a82bed63a57240aa87a7cd0a83122cc1e75.tar.bz2 st-scripts-64851a82bed63a57240aa87a7cd0a83122cc1e75.zip |
Some more additions to basic python ended discussion on string started operators
Diffstat (limited to 'basic-python.txt')
-rw-r--r-- | basic-python.txt | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/basic-python.txt b/basic-python.txt index 5fa2a8d..eb42deb 100644 --- a/basic-python.txt +++ b/basic-python.txt @@ -84,7 +84,72 @@ Also functions like len work with strings just like the way they did with lists Now lets try changing a character in the string in the same way we change lists . type : -w[0]='Capital H' +w[0]='H' oops this gives us a Type Error . Why? Because string are immutable . You can change a string simply by assigning a new element to it . This and some other features specific to string processing make string a different kind of data structure than lists . +Now lets see some of the ways in which you can modify strings and other methods related to strings . + +Type : + +a = 'Hello world' + +To check if a particular string starts with a particular substring you can check that with startswith method + +a.startswith('Hell') + +Depending on whether the string starts with that substring the function returns true or false + +same is the case a.endwith('ld') + +a.upper() + returns another string that is all the letters of given string capitalized + +similarly a.lower returns all small letters . + +Earlier we showed you how to see documentations of functions . You can see the documentation of the lower function by doing a.lower? + +You can use a.join to joing a list of strings to one string using a given string as connector . + +for example + +type : +', '.join(['a','b','c']) + +In this case strings are joined over , and space + +Python supports formatting values into strings. Although this can include very complicated expressions, the most basic usage is to insert values into a string with the %s placeholder. %d can be used for formatting things like integers and %f for floats + + +Their are many other string formatting options you can look at http://docs.python.org/library/stdtypes.html for more information on other options available for string formatting. + + +Operators ---- Probably can be a different chapter . + +We will start the discussion on operators first with arithmetic operators . + +% can be used for remainder for example + +864675 % 10 gives remainder 5 + + +you can use 2 *'s for power operation + +for example 4 ** 3 gives the result 64 + +One thing one should notice is the type of result depends on the types of input for example : + +17 / 2 both the values being integer gives the integer result 2 + +however the result when one or two of the operators are float is float for example: + +17/2.0 +8.5 +17.0/2.0 +8.5 + + + + + + |