String Methods
- Slicing
With slicing we can access substrings. It can get optional start and stop indices.
a='pythonfullstack'
a= a[3:9]
# 'honful'
- strip()
strip() method makes the string Return a copy of the string with the leading and trailing characters removed. default characters of this method removes whitespace
a= ' python '
a= a.strip()
#'python'
- replace() Return a copy of the string with all occurrences of substring old replaced by new.
a= ' hi folks'
a= a.replace('hi','hello')
# ' hello folks'
- split() split() methods uses for returning the list of words in a string, using sep as the delimiter string
a= ' hi folks good morning'
a= a.split()
# ['hi', 'folks', 'good' ,'morning']
- join() Return a string which is the concatenation of the strings in iterable.
a=['hi', 'folks', 'good' ,'morning']
a= ' '.join(a)
# 'hi folks good morning'
- upper(), lower(), capitalize() Return a copy of the string with all the cased characters converted to uppercase, lowercase, or first character capitalized and the rest lowercased.
a= 'python'
a= a.upper()
#'PYTHON'
a='pyThOn'
a= a.lower()
# 'python'
a= 'pyTHoN'
a=a.capitalize()
# 'Python'
- isalpha(), isnumeric(), isalnum()
isalpha() is to check a string whether it contains alphabets only.
isnumeric() is to check a string whether it contains numbers only.
isalnum() is to check a string whether it contains alphabets or numbers.
s= 'justchill'
s.isalpha() #output : True
s='9887'
s.isnumeric() # output: True
s='30jno'
s.isalnum() #output: True
- count() count() method is used to count the particular substrings in a string.
s='hello'
s.count('l') # 2
- find() Return the lowest index in the string where substring sub is found within the slice s[start:end]
s= 'run python'
ind= s.find('y') # 5
- swapcase()
Return a copy of the string with uppercase characters converted to lowercase and vice versa.
s= 'HeLLo'
s=s.swapcase()
# 'hEllO'
Top comments (0)