String Methods
upper():
The method upper takes a string and returns a new string with all uppercase letters.
>>> word = 'banana'>>> new_word = word.upper()>>> new_word'BANANA'
lower():
The method lower takes a string and returns a new string with all lowercase letters.
>>> word = 'BANANA'>>> new_word = word.lower()>>> new_word'banana'
find():
The method find search for a specific character or characters in a string.
>>> word = 'banana'>>> index = word.find('a')>>> index1>>> word.find('na')2
By default, find starts at the beginning of the string, but it can take a second argument, the index where it should start:
>>> word.find('na', 3)4
count()
The method count adds up the number of times a character or sequence of characters appears in a string.
>>> s = "The the">>> s.count("t")1
capitalize()
This method capitalize the first character and the remaining characters in lowercase format.
>>> s=”west”>>> new_s = s.capitalize()>>> new_sWest
isalnum():
The method isalnum() checks whether the string consists of alphanumeric characters.
>>> str='sadsaf12412'>>> print(str.isalnum())True
isalpha():
The method isalpha() checks whether the string consists of alphabetic characters only.
>>> s="dhghg66">>> print(s.isalpha())False
References
- Allen B. Downey, “Think Python: How to Think Like a Computer Scientist‘‘, 2nd edition, Updated for Python 3, Shroff/O‘Reilly Publishers, 2016 (http://greenteapress.com/wp/thinkpython/)
- Guido van Rossum and Fred L. Drake Jr, ―An Introduction to Python – Revised and updated for Python 3.2, Network Theory Ltd., 2011.
- John V Guttag, ―Introduction to Computation and Programming Using Python‘‘, Revised and expanded Edition, MIT Press , 2013
- Robert Sedgewick, Kevin Wayne, Robert Dondero, ―Introduction to Programming in Python: An Inter-disciplinary Approach, Pearson India Education Services Pvt. Ltd., 2016.
- Timothy A. Budd, ―Exploring Python‖, Mc-Graw Hill Education (India) Private Ltd.,, 2015. 4. Kenneth A. Lambert, ―Fundamentals of Python: First Programs‖, CENGAGE Learning, 2012.
- Charles Dierbach, ―Introduction to Computer Science using Python: A Computational Problem-Solving Focus, Wiley India Edition, 2013.
- Paul Gries, Jennifer Campbell and Jason Montojo, ―Practical Programming: An Introduction to Computer Science using Python 3‖, Second edition, Pragmatic Programmers, LLC, 2013.