Strings in python are enclosed in single quotes('..') or double quotes("..").
print("Hello World!")
print('Python')
You can use three single quotes or three double quotes to assign multi line string to a variable.
var = '''The output string is
enclosed in quotes and
special characters are escaped with backslashes.'''
print(var)
- String Length len()
To get the length of a string, use the len() function.
print(len(var))
- split()
To get a list of substrings from the given string we use split() function.
Syntax: str.split(separator, maxsplit)
str1 = "Hello World"
print(str1.split())
If we don't specify any separator it takes space as default value.
Maxsplit is used to specify number of splits we need and it's default value is -1.
- join()
To join a list of substrings into a single string we use join() function.
Syntax: str.join(iterable)
list_1 = ['Hello','World!']
string1 = " ".join(list_1)
- index()
To get the index of a substring we use index() function.
str1 = 'Python Programming'
print(str1.index('i'))
It raises an ValueError exception if substring is not found.
- lower()
Converts all uppercase letters in string to lowercase.
str1 = 'Python Programming'
print(str1.lower())
- upper()
Converts all lowercase letters in string to uppercase.
str1 = 'Python Programming'
print(str1.upper())
- islower()
Returns True only when all the characters in string are in lowercase and False otherwise.
str1 = 'Python Programming'
print(str1.islower())
- isupper()
Returns True only when all the characters in string are in uppercase and False otherwise.
str1 = 'Python Programming'
print(str1.isupper())
- isdigit()
Returns True only when all the characters in string are digits and False otherwise.
str1 = '123'
print(str1.isdigit())
- isalpha()
Returns True only when all the characters in string are alphabetic and False otherwise.
str1 = 'Python Programming'
print(str1.isalpha())
- capitalize()
Converts the first character in string to upper case and all other characters to lowercase.
str1 = 'python'
print(str1.capitalize())
- casefold()
Returns a copy of string in lowercase.
str1 = 'Python Programming'
print(str1.casefold())
- endswith()
Returns True if the string ends with the specified suffix, otherwise False.
str1 = 'Python Programming'
print(str1.endswith('ing'))
- startswith()
Returns True if the string starts with the specified prefix, otherwise False.
str1 = 'Python Programming'
print(str1.startswith('Pyth'))
- swapcase()
Returns a string with uppercase characters converted to lowercase and vice versa.
str1 = 'pYTHON pROGRAMMING'
print(str1.swapcase())
- strip()
Returns a copy of the string with leading and trailing characters removed.
Syntax: str.strip([chars])
The chars argument is a string specifying the set of characters to be removed.
By default chars set to remove whitespace.
str1 = ' Python '
print(str1.strip())
- replace()
Syntax: str.replace(old, new)
Returns a copy of the string with all occurrences of old substring replaces by new substring.
str1 = 'Python Programming'
print(str1.replace('Python', 'Java'))
Reference:
String Methods
Top comments (0)