Python Programming Language - II

Python Programming Language


Python Training | Python programming, Python logo, Programming ...


Python String Function

capitalize() - It converts the first character to upper case.
Example:- 
                st = passable
                print(st.capitalize())
Output:-
               Passable

center() - It returns a center aligned string.
Example:-
                st = "PassableBlog"
                p = st.center(20)
                print(p)
Output:-
                       PassableBlog           

count() - It returns the number of times a specified character occurs in a string.
Example:-
                st = 'This is Passable Blog. Please visit.'
                print(st.count('P'))
Output:-
                2

endswith() - It returns true if the string ends with the specified value.
Example:- 
                st = 'This is Passable blog'
                print(st.endswith('blog'))
Output:-
                True

isalnum() - It returns True if all characters in the string are alphanumeric.
Example:- 
                st = 'A2p'
                print(st.isalnum())
Output:-
                True

isalpha() - It returns True if all characters in the string are alphabets.
Example:-
                st = 'Passableblog'
                print(st.isalpha())
Output:-
                True

isdigit() - It returns True if all characters in the string are digits.
Example:-
                st = '245698'
                print(st.isdigit())
Output:-
                True

islower() - It returns True if all characters in the string are lower case.
Example:-
                st = 'passableblog'
                print(st.islower())
Output:-
                True

isupper() - It returns True if all characters in the string are upper case.
Example:-
                st = 'PASSABLEBLOG'
                print(st.isupper())
Output:-
                True

join() - It joins the elements of a list to the end of the string.
Example:-
                l = ['This', 'is', 'Passable', 'Blog']
                '  '.join(l)
                print(l)
Output:-
                This is Passable Blog

split() - It splits the string and returns a list.
Example:- 
                st = 'Passableblog'
                st.split(',')
                print(st)
Output:-
                ['P','a','s','s','a','b','l','e','b','l','o','g']

startswith() - It returns True if the string starts with the specified value.
Example:-
                st = 'This is Passable Blog'
                print(st.startswith('This'))
Output:-
               True

swapcase() - It converts the lower case letters to upper case and vice versa.
Example:-
                st = 'PaSSable'
                print(st.swapcase())
Output:-
                pAssABLE

title() - It converts the first character of each word to upper case.
Example:-
                st = 'passable blog'
                print(st.title())
Output:-
                Passable Blog

Python List Functions

append() - It adds an element at the end of the list.
Example:-
                l = [1,2,3]
                a = 4
                l.append(a)
                print(l)
Output:-
                [1,2,3,4]

clear() - It removes all the elements from the list.

copy() - It returns a copy of the list.
Example:-
                l = [1,2,3,4]
                p = l.copy()
                print(p)
Output:-
                [1,2,3,4]

count() - It returns the number of elements with the specified value.
Example:-
                l = ['a','b','a','c']
                x = l.count('a')
                print(x)
Output:-
                2

insert() - It adds an element at the specified position,
Example:- 
            l = ['a','b','c']
            l.insert(0,'d')
            print(l)
Output:-
            ['d','a','b','c']

pop() - It removes the element at the specified position.
Example:-
            l = ['a','b','c']
            l.pop(1)
            print(l)
Output:-
            ['a','c']

remove() - It removes the first item with the specified value.
Example:- 
            l = [1,2,3,4]
            l.remove(1)
            print(l)
Output:-
            [2,3,4]

sort() - It sorts the list.
Example;-
            l = [2,9,1,3]
            print(l.sort())
Output:-
            [1,2,3,9]

To be continued..

Comments

Post a Comment

Popular posts from this blog

Some tips about the right way to charge your phone

Software Basics

Some unique and weird things about Japan