Python Tutorial
#!/usr/bin/python input_string = "welcome to python strings!"; print("Number of characters: %d" % len(input_string));Output:
$ python test_string_methods.py Number of characters: 26
#!/usr/bin/python input_string = "welcome to python strings!"; print("Substring occurence count: %d" % input_string.count("o"));Output:
$ python test_string_methods.py Substring occurence count: 3we can also specify begining index and ending index to check.
#!/usr/bin/python input_string = "welcome to python strings!"; print("Substring occurence count: %d" % input_string.count("o", 0, 10));Output:
$ python test_string_methods.py Substring occurence count: 2if not found any occurrence, returns 0.
#!/usr/bin/python input_string = "welcome to python strings!"; print("Substring occurence count: %d" % input_string.count("z", 0, 10));Output:
$ python test_string_methods.py Substring occurence count: 0
#!/usr/bin/python input_string = "welcome to python strings welcome to!"; print("replace method in input_string?: %s" % input_string.replace("welcome to", "learning"));Output:
$ python test_string_methods.py replace method in input_string?: learning python strings learning!
#!/usr/bin/python input_string = "welcome to python strings welcome to!"; print("split method in input_string?: %s" % input_string.split(" ")); print("split method with limit 2 in input_string?: %s" % input_string.split(" ", 2));Output:
$ python test_string_methods.py split method in input_string?: ['welcome', 'to', 'python', 'strings', 'welcome', 'to!'] split method with limit 2 in input_string?: ['welcome', 'to', 'python strings welcome to!']
#!/usr/bin/python input_string = "welcome to python strings!"; print(input_string.capitalize());Output:
$ python test_string_methods.py Welcome to python strings!
#!/usr/bin/python input_string = "welcome to python strings!"; print(input_string.startswith("welcome"));Output:
$ python test_string_methods.py Truewe can also specify begining index and end index range to check.
#!/usr/bin/python input_string = "welcome to python strings!"; print(input_string.startswith("welcome", 10, len(input_string)));Output:
$ python test_string_methods.py False
#!/usr/bin/python input_string = "welcome to python strings!"; print(input_string.endswith("strings!"));Output:
$ python test_string_methods.py Truewe can also specify begining index and end index range to check.
#!/usr/bin/python input_string = "welcome to python strings!"; print(input_string.endswith("strings!", 0, len(input_string)-10));Output:
$ python test_string_methods.py False
#!/usr/bin/python input_string = "welcome to python strings!"; print(input_string.index("strings!"));Output:
$ python test_string_methods.py 18we can also specify begining and ending index to check
#!/usr/bin/python input_string = "welcome to python strings!"; print(input_string.index("strings!", 0, 20));Output:
$ python test_string_methods.py Traceback (most recent call last): File "test_string_methods.py", line 5, inprint(input_string.index("strings!", 0, 20)); ValueError: substring not found
#!/usr/bin/python input_string = "welcome to python strings!"; print(input_string.find("strings!"));Output:
$ python test_string_methods.py 18
#!/usr/bin/python input_string = "welcome to python strings!"; print(input_string.find("strings!", 0, 20));Output:
$ python test_string_methods.py -1
#!/usr/bin/python input_string = "welcome to python strings!"; print "is input_string alphanumeric?:", input_string.isalnum();Output:
$ python test_string_methods.py is input_string alphanumeric?: False
#!/usr/bin/python input_string = "thisnumber2344"; print "is input_string alphanumeric?:", input_string.isalnum();Output:
$ python test_string_methods.py is input_string alphanumeric?: TrueNo space and symbols in the string.
#!/usr/bin/python input_string = "welcome"; print "is input_string alpha:", input_string.isalpha();Output:
$ python test_string_methods.py is input_string alpha: True
#!/usr/bin/python input_string = "welcome to python strings!"; print "is input_string alpha:", input_string.isalpha();Output:
$ python test_string_methods.py is input_string alpha: False
#!/usr/bin/python input_string = "2344"; print "is input_string digits?: ", input_string.isdigit();Output:
$ python test_string_methods.py is input_string digits?: True
#!/usr/bin/python input_string = "welcome to python strings!"; print "is input_string digits?: ", input_string.isdigit();Output:
$ python test_string_methods.py is input_string digits?: False
#!/usr/bin/python input_string = "welcome to python strings!"; print "is lower input_string?: ", input_string.islower();Output:
$ python test_string_methods.py is lower input_string?: True
#!/usr/bin/python input_string = "Welcome to python strings!"; print "is lower input_string?: ", input_string.islower();Output:
$ python test_string_methods.py is lower input_string?: False
#!/usr/bin/python input_string = "WELCOME TO PYTHON STRINGS!"; print "is upper case letters only in input_string?: ", input_string.isupper();Output:
$ python test_string_methods.py is upper case letters only in input_string?: True
#!/usr/bin/python input_string = "Welcome to python strings!"; print "is upper case letters only in input_string?: ", input_string.isupper();Output:
$ python test_string_methods.py iis upper case letters only in input_string?: False
#!/usr/bin/python input_string = "22343"; print("is numeric only in input_string?: %s" % str(input_string.isnumeric()));Output:
$ python3 test_string_methods.py is numeric only in input_string?: True
#!/usr/bin/python input_string = "Welcome to python strings!"; print("is numeric only in input_string?: %s" % str(input_string.isnumeric()));Output:
$ python3 test_string_methods.py is numeric only in input_string?: FalseNeed to use python3 interpreter for isnumeric method, otherwise below error thrown
]$ python test_string_methods.py Traceback (most recent call last): File "test_string_methods.py", line 5, inprint("is numeric only in input_string?: %s" % str(input_string.isnumeric())); AttributeError: 'str' object has no attribute 'isnumeric'
#!/usr/bin/python input_string = " "; print("is spaces only in input_string?: %s" % str(input_string.isspace()));Output:
$ python test_string_methods.py is spaces only in input_string?: True
#!/usr/bin/python input_string = "Welcome to Python Strings! "; print("lower case input_string?: %s" % input_string.lower());Output:
$ python test_string_methods.py lower case input_string?: welcome to python strings!
#!/usr/bin/python input_string = "Welcome to Python Strings! "; print("upper case input_string?: %s" % input_string.upper());Output:
$ python test_string_methods.py upper case input_string?: WELCOME TO PYTHON STRINGS!
#!/usr/bin/python input_string = " welcome to Python Strings! "; print("lstrip input_string?: %s" % input_string.lstrip()); print("lstrip input_string?: %s" % input_string.lstrip(" wel"));Output:
$ python test_string_methods.py lstrip input_string?: welcome to Python Strings! lstrip input_string?: come to Python Strings!
#!/usr/bin/python input_string = "welcome to Python Strings! "; print("rstrip input_string?: %s" % input_string.rstrip()); print("rstrip input_string?: %s" % input_string.rstrip(" gs!"));Output:
$ python test_string_methods.py rstrip input_string?: welcome to Python Strings! rstrip input_string?: welcome to Python Strin
#!/usr/bin/python input_string = " welcome to python strings! "; print("strip method in input_string?: %s" % input_string.strip()); print("strip method with chars in input_string?: %s" % input_string.strip(" welngs!"));Output:
]$ python test_string_methods.py strip method in input_string?: welcome to python strings! strip method with chars in input_string?: come to python stri
#!/usr/bin/python from string import maketrans; input_string = "Welcome to Python Strings! "; transtab = maketrans("omt", "123"); print("maketrans method for input_string?: %s" % input_string.translate(transtab));Output:
$ python test_string_methods.py maketrans method for input_string?: Welc12e 31 Py3h1n S3rings!
#!/usr/bin/python input_string = "Welcome to Python Strings!"; print("max char in input_string?: %s" % max(input_string));Output:
$ python test_string_methods.py max char in input_string?: y
#!/usr/bin/python input_string = "welcome"; print("min char in input_string?: %s" % min(input_string));Output:
$ python test_string_methods.py min char in input_string?: c
Python Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page