Here are seven Python string manipulation methods (functions). Those are Find, Join, Validation, Change case, Split, Replace, and Stripe.
Note: In python, there is no CHAR data type. So only strings you need to use.
Useful String functions in Python
Srinimf
Python string functions
1. Python string find
Neat function to find specific string if it is present or not.
text = 'This is text'
nums = '123456'
# finding text
a= text.find('is')
b= text.find('your')
print(a)
print(b)
Output
2
-1
** Process exited - Return Code: 0 **
2. Python validate a string
Helpful to do validation checks on the strings.
text = 'This is text'
nums = '123456'
# Validation checks
print(text.isalpha())
print(text.isdigit())
print(nums.isdigit())
Output
False
False
True
** Process exited - Return Code: 0 **
3. Python string concatenation
Useful to concatenate strings with or without separator.
text = 'This is text'
nums = '123456'
a=''.join((text,nums))
b=' '.join((text,nums))
print(a)
print(b)
Output
This is text123456
This is text 123456
** Process exited - Return Code: 0 **
#Can you try?
Q.1)
str1 = ‘XYZ’
str2 = ‘1234’
print(str1.join(str2))
print(str2.join(str1))
Q.2)
string = ‘Hello’
print(‘.’.join(string))
4. Python change case of a string
Useful to convert case of the string.
text = 'This is text'
# case changing
print(text.upper())
print(text.lower())
Output
THIS IS TEXT
this is text
** Process exited - Return Code: 0 **
5. Python string split
Effective to Split string into words.
# splitting string
text = 'This is text'
a=text.split(' ')
print(a)
Output
['This', 'is', 'text']
** Process exited - Return Code: 0 **
6. Python replace string
Handy method to replace part of the string with the other one.
text = 'This is text'
# substitution
a=text.replace('is','was')
print(a)
Output
Thiswas text
** Process exited - Return Code: 0 **
7. Python stripe string
Advantageous to remove space in trailing and leading edges and both.
text1 = 'This is text '
a=text1.rstrip() # remove trailing space
text2 = ' This is text'
b=text2.lstrip() # remove leading space
text3 = ' This is text '
c=text3.strip() # remove trailing and leading spaces
print(a)
print(b)
print(c)
Output
This is text
This is text
This is text
** Process exited - Return Code: 0 **
Related posts