Strings in Python
Yaswanth K N
Posted on May 11, 2023
The Purpose of this paper is to get required idea of methods of String in python in simplified way
String is a collection of characters.
Strings are immutable in python that means we can not change values of string but to create new string.
we can access each character by their index
Methods:
-
string.strip()
This method will delete the trailing and leading spaces of string
string = " yaswanth " #Notice there are spaces before and after the string name = string.strip() print(name) #output : 'yaswanth'
-
string.split()
This method is overloaded, if we don't give any arguments to split() it will split the string at spaces and returns the list of strings.
if we give argument to it, it will split the string at that character and returns list of strings
string = " hellow world " ls = string.split() print(ls) #output ['hellow','world'] string = string.strip() ls = string.split('o) print(ls) #output ['hell','w w','rld']
-
string.lower()
It will convert the every alphabet in string to lower case
string = 'YASWANTH' lower_case = string.lower() print(lower_case) #output 'yaswanth'
-
string.upper()
It will convert the every alphabet in string to upper case
string = 'yaswanth' upper_case = string.upper() print(lower_case) #output 'YASWANTH'
-
string.isdigit()
It will return a boolean value, if entire string is number returns true, else returns false.
string = '12345' print(string.isdigit()) #prints True string = ' 12345 ' #Notice there a space in front and back of string print(string.isdigit()) #prints False
-
string.isupper()
This will return true if all the alphabets of string are upper case else it will return false
string = 'HELLO WORLD' print(string.isupper()) #prints TRUE string = 'Hello world' print(string.isupper()) #prints FALSE
-
string.islower()
This will return true if all the alphabets of string are lower case else it will return false
string = 'HELLO WORLD' print(string.isupper()) #prints FALSE string = 'hello world' print(string.isupper()) #prints TRUE
-
string.join(iterable)
This function takes the iterable as a argument
This will join string to each item in the iterable and returns string
string = 'hello' string = ' '.join(string) print(string) # prints 'h e l l o ' as output
-
string.startswith('object')
This function returns True if string starts with given object else returns False
string = 'hello, I am Yaswanth' print(string.startswith('hello')) #returns True
-
string.index('object')
This function returns the start index of object in string
string = 'hey! There' print(string.index('There')) #returns 5
-
string.isalpha()
This function retuns boolean value. it will return True if all characters in string are alphabets else returns false.
string = 'hello' print(string.isalpha()) #returns True string = '1 hello 2' print(string.isalpha()) #retuns False
-
string.format(arg1,arg2..)
This function is used to format a string by sending arguments to string which are not necessorly strings.
Although we have string literal to do this work now it's always good to know legacy code
name = 'yaswanth' age = 21 print('my name is {}, my age is {}'.format(name,age)) # prints 'my name is yaswanth, my age is 22'
References:
Posted on May 11, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024
November 30, 2024