DEV Community

Ravi Shankar
Ravi Shankar

Posted on

String Methods

Strings in python are enclosed in single quotes('..') or double quotes("..").

print("Hello World!")
print('Python')
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode
  • String Length len()

To get the length of a string, use the len() function.

print(len(var))
Enter fullscreen mode Exit fullscreen mode
  • 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())
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode
  • index()

To get the index of a substring we use index() function.

str1 = 'Python Programming'
print(str1.index('i'))
Enter fullscreen mode Exit fullscreen mode

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())
Enter fullscreen mode Exit fullscreen mode
  • upper()

Converts all lowercase letters in string to uppercase.

str1 = 'Python Programming'
print(str1.upper())
Enter fullscreen mode Exit fullscreen mode
  • islower()

Returns True only when all the characters in string are in lowercase and False otherwise.

str1 = 'Python Programming'
print(str1.islower())
Enter fullscreen mode Exit fullscreen mode
  • isupper()

Returns True only when all the characters in string are in uppercase and False otherwise.

str1 = 'Python Programming'
print(str1.isupper())
Enter fullscreen mode Exit fullscreen mode
  • isdigit()

Returns True only when all the characters in string are digits and False otherwise.

str1 = '123'
print(str1.isdigit())
Enter fullscreen mode Exit fullscreen mode
  • isalpha()

Returns True only when all the characters in string are alphabetic and False otherwise.

str1 = 'Python Programming'
print(str1.isalpha())
Enter fullscreen mode Exit fullscreen mode
  • capitalize()

Converts the first character in string to upper case and all other characters to lowercase.

str1 = 'python'
print(str1.capitalize())
Enter fullscreen mode Exit fullscreen mode
  • casefold()

Returns a copy of string in lowercase.

str1 = 'Python Programming'
print(str1.casefold())
Enter fullscreen mode Exit fullscreen mode
  • endswith()

Returns True if the string ends with the specified suffix, otherwise False.

str1 = 'Python Programming'
print(str1.endswith('ing'))
Enter fullscreen mode Exit fullscreen mode
  • startswith()

Returns True if the string starts with the specified prefix, otherwise False.

str1 = 'Python Programming'
print(str1.startswith('Pyth'))
Enter fullscreen mode Exit fullscreen mode
  • swapcase()

Returns a string with uppercase characters converted to lowercase and vice versa.

str1 = 'pYTHON pROGRAMMING'
print(str1.swapcase())
Enter fullscreen mode Exit fullscreen mode
  • 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())
Enter fullscreen mode Exit fullscreen mode
  • 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'))
Enter fullscreen mode Exit fullscreen mode

Reference:
String Methods

Top comments (0)