Skip to content

Python String Methods

Python’s built-in str class defines different methods. These methods can be used to manipulate strings. In this tutorial, we will learn about the different methods available for Python strings.

These are some of the most commonly used string methods:

Sequence
Function Name
Description
1capitalize()capitalize()Converts the first character to upper case
2casefold()casefold()Converts string into lower case
3center()center()Returns a centered string
4count()count()Returns the number of times a specified value occurs in a string
5encode()encode()Returns an encoded version of the string
6endswith()endswith()Returns true if the string ends with the specified value
7expandtabs()expandtabs()Sets the tab size of the string
8find()find()Searches the string for a specified value and returns the position of where it was found
9format()format()Formats specified values in a string
10format_map()format_map()Formats specified values in a string
11index()index()Searches the string for a specified value and returns the position of where it was found
12isalnum()isalnum()Returns True if all characters in the string are alphanumeric
13isalpha()isalpha()Returns True if all characters in the string are in the alphabet
14isdecimal()isdecimal()Returns True if all characters in the string are decimals
15isdigit()isdigit()Returns True if all characters in the string are digits
16isidentifier()isidentifier()Returns True if the string is an identifier
17islower()islower()Returns True if all characters in the string are lower case
18isnumeric()isnumeric()Returns True if all characters in the string are numeric
19isprintable()isprintable()Returns True if all characters in the string are printable
20isspace()isspace()Returns True if all characters in the string are whitespaces
21istitle()istitle()Returns True if the string follows the rules of a title
22isupper()isupper()Returns True if all characters in the string are upper case
23join()join()Joins the elements of an iterable to the end of the string
24ljust()ljust()Returns a left justified version of the string
25lower()lower()Converts a string into lower case
26lstrip()lstrip()Returns a left trim version of the string
27maketrans()maketrans()Returns a translation table to be used in translations
28partition()partition()Returns a tuple where the string is parted into three parts
29replace()replace()Returns a string where a specified value is replaced with a specified value
30rfind()rfind()Searches the string for a specified value and returns the last position of where it was found
31rindex()rindex()Searches the string for a specified value and returns the last position of where it was found
32rjust()rjust()Returns a right justified version of the string
33rpartition()rpartition()Returns a tuple where the string is parted into three parts
34rsplit()rsplit()Splits the string at the specified separator, and returns a list
35rstrip()rstrip()Returns a right trim version of the string
36split()split()Splits the string at the specified separator, and returns a list
37splitlines()splitlines()Splits the string at line breaks and returns a list
38startswith()startswith()Returns true if the string starts with the specified value
39strip()strip()Returns a trimmed version of the string
40swapcase()swapcase()Swaps cases, lower case becomes upper case and vice versa
41title()title()Converts the first character of each word to upper case
42translate()translate()Returns a translated string
43upper()upper()Converts a string into upper case
44zfill()zfill()Fills the string with a specified number of 0 values at the beginning
45++Concatenates two strings
46**Returns a string repeated the specified number of times
47[][]Returns the character at the specified index
48[:][:]Returns the slice from the specified index to the specified index
49ininReturns True if a sequence with the specified value is present in the object
50not innot inReturns True if a sequence with the specified value is not present in the object
51%%Formats specified values in a string
52<<Returns True if the first string is lower than the second string
53<=<=Returns True if the first string is lower than or equal to the second string
54>>Returns True if the first string is greater than the second string
55>=>=Returns True if the first string is greater than or equal to the second string
56====Returns True if the first string is equal to the second string
57!=!=Returns True if the first string is not equal to the second string
58ord()ord()Converts a character into Unicode
59hex()hex()Converts an integer to a hexadecimal string
60oct()oct()Converts an integer to an octal string
61bin()bin()Converts an integer to a binary string
62chr()chr()Converts an integer to a character
63len()len()Returns the length of the string
64repr()repr()Returns a readable version of the string
65ascii()ascii()Returns a readable version of the string
66max()max()Returns the largest character in the string
67min()min()Returns the smallest character in the string
68str()str()Returns a string object
69type()type()Returns the type of the specified object
70help()help()Executes the built-in help system

1. capitalize()

The capitalize()capitalize() method returns a string where the first character is upper case.

Syntax:

string.capitalize()string.capitalize()

  • Returns - A capitalized string

Example:

strings.py
# capitalize() method
string = 'python strings'
print(string.capitalize())
strings.py
# capitalize() method
string = 'python strings'
print(string.capitalize())

Output

command
C:\Users\Your Name> python strings.py
Python strings
command
C:\Users\Your Name> python strings.py
Python strings

2. casefold()

The casefold()casefold() method returns a string where all the characters are lower case.

Syntax:

string.casefold()string.casefold()

  • Returns - A lower case string

Example:

strings.py
# casefold() method
string = 'Python Strings'
print(string.casefold())
strings.py
# casefold() method
string = 'Python Strings'
print(string.casefold())

Output

command
C:\Users\Your Name> python strings.py
python strings
command
C:\Users\Your Name> python strings.py
python strings

3. center()

The center()center() method will center align the string, using a specified character (space is default) as the fill character.

Syntax:

string.center(length, character)string.center(length, character)

  • length - The length of the returned string
  • character (optional) - The character to fill the missing space on each side. Default is " "" "
  • Returns - A centered string

Example:

strings.py
# center() method
string = 'Python Strings'
print(string.center(20))
print(string.center(20, '*'))
strings.py
# center() method
string = 'Python Strings'
print(string.center(20))
print(string.center(20, '*'))

Output

command
C:\Users\Your Name> python strings.py
   Python Strings
***Python Strings***
command
C:\Users\Your Name> python strings.py
   Python Strings
***Python Strings***

4. count()

The count()count() method returns the number of times a specified value appears in the string.

Syntax:

string.count(value, start, end)string.count(value, start, end)

  • value - The value to search for
  • start (optional) - The position to start the search. Default is 00
  • end (optional) - The position to end the search. Default is len(string)len(string)
  • Returns - The number of times the value appears in the string

Example:

strings.py
# count() method
string = 'Python Strings'
print(string.count('s'))
print(string.count('s', 7, 14))
strings.py
# count() method
string = 'Python Strings'
print(string.count('s'))
print(string.count('s', 7, 14))

Output

command
C:\Users\Your Name> python strings.py
2
1
command
C:\Users\Your Name> python strings.py
2
1

5. encode()

The encode()encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.

Syntax:

string.encode(encoding, errors)string.encode(encoding, errors)

  • encoding (optional) - A String specifying the encoding to use. Default is UTF-8UTF-8
  • errors (optional) - A String specifying the error method. Legal values are:
    • backslashreplacebackslashreplace - uses a backslash instead of the character that could not be encoded
    • ignoreignore - ignores the characters that cannot be encoded
    • namereplacenamereplace - replaces the character with a text explaining the character
    • strictstrict - Default, raises an error on failure
    • replacereplace - replaces the character with a questionmark
    • xmlcharrefreplacexmlcharrefreplace - replaces the character with an xml character

Example:

strings.py
# encode() method
string = 'Python Strings'
print(string.encode())
print(string.encode(encoding='ascii', errors='ignore'))
strings.py
# encode() method
string = 'Python Strings'
print(string.encode())
print(string.encode(encoding='ascii', errors='ignore'))

Output

command
C:\Users\Your Name> python strings.py
b'Python Strings'
b'Python Strings'
command
C:\Users\Your Name> python strings.py
b'Python Strings'
b'Python Strings'

6. endswith()

The endswith()endswith() method returns TrueTrue if the string ends with the specified value, otherwise FalseFalse.

Syntax:

string.endswith(value, start, end)string.endswith(value, start, end)

  • value - Required. The value to check if the string ends with
  • start (optional) - Optional. An Integer specifying at which position to start the search
  • end (optional) - Optional. An Integer specifying at which position to end the search
  • Returns - TrueTrue if the string ends with the specified value, otherwise FalseFalse

Example:

strings.py
# endswith() method
string = 'Python Strings'
print(string.endswith('s'))
print(string.endswith('s', 7, 14))
strings.py
# endswith() method
string = 'Python Strings'
print(string.endswith('s'))
print(string.endswith('s', 7, 14))

Output

command
C:\Users\Your Name> python strings.py
True
False
command
C:\Users\Your Name> python strings.py
True
False

7. expandtabs()

The expandtabs()expandtabs() method sets the tab size to the specified number of whitespaces.

Syntax:

string.expandtabs(tabsize)string.expandtabs(tabsize)

  • tabsize (optional) - A number specifying the tabsize. Default tabsize is 88
  • Returns - A string where all \t\t characters are replaced with whitespaces using the specified tabsize

Example:

strings.py
# expandtabs() method
string = 'Python\tStrings'
print(string.expandtabs())
print(string.expandtabs(2))
print(string.expandtabs(4))
strings.py
# expandtabs() method
string = 'Python\tStrings'
print(string.expandtabs())
print(string.expandtabs(2))
print(string.expandtabs(4))

Output

command
C:\Users\Your Name> python strings.py
Python  Strings
Python  Strings
Python    Strings
command
C:\Users\Your Name> python strings.py
Python  Strings
Python  Strings
Python    Strings

8. find()

The find()find() method finds the first occurrence of the specified value. The find()find() method returns -1-1 if the value is not found.

Syntax:

string.find(value, start, end)string.find(value, start, end)

  • value - Required. The value to search for
  • start (optional) - Optional. Where to start the search. Default is 00
  • end (optional) - Optional. Where to end the search. Default is len(string)len(string)
  • Returns - The index of the first occurrence of the specified value

Example:

strings.py
# find() method
string = 'Python Strings'
print(string.find('s'))
print(string.find('s', 7, 14))
strings.py
# find() method
string = 'Python Strings'
print(string.find('s'))
print(string.find('s', 7, 14))

Output

command
C:\Users\Your Name> python strings.py
7
-1
command
C:\Users\Your Name> python strings.py
7
-1

9. format()

The format()format() method formats the specified value(s) and insert them inside the string’s placeholder.

Syntax:

string.format(value1, value2...)string.format(value1, value2...)

  • value1, value2… - Optional. A value to be formatted and inserted into the string’s placeholder
  • Returns - A formatted string

Example:

strings.py
# format() method
string = 'Python Strings'
print('I love {}'.format(string))
print('I love {0}'.format(string))
strings.py
# format() method
string = 'Python Strings'
print('I love {}'.format(string))
print('I love {0}'.format(string))

Output

command
C:\Users\Your Name> python strings.py
I love Python Strings
I love Python Strings
command
C:\Users\Your Name> python strings.py
I love Python Strings
I love Python Strings

10. format_map()

The format_map()format_map() method formats the specified value(s) and insert them inside the string’s placeholder.

Syntax:

string.format_map(map)string.format_map(map)

  • map - Required. A dictionary containing the variables to insert into the string’s placeholder
  • Returns - A formatted string

Example:

strings.py
# format_map() method
string = 'Python Strings'
print('I love {name}'.format_map({'name': string}))
print('I love {name}'.format_map({'name': 'Python'}))
strings.py
# format_map() method
string = 'Python Strings'
print('I love {name}'.format_map({'name': string}))
print('I love {name}'.format_map({'name': 'Python'}))

Output

command
C:\Users\Your Name> python strings.py
I love Python Strings
I love Python
command
C:\Users\Your Name> python strings.py
I love Python Strings
I love Python

11. index()

The index()index() method finds the first occurrence of the specified value. The index()index() method raises an exception if the value is not found.

Syntax:

string.index(value, start, end)string.index(value, start, end)

  • value - Required. The value to search for
  • start (optional) - Optional. Where to start the search. Default is 00
  • end (optional) - Optional. Where to end the search. Default is len(string)len(string)
  • Returns - The index of the first occurrence of the specified value

Example:

strings.py
# index() method
string = 'Python Strings'
print(string.index('s'))
print(string.index('s', 7, 14))
strings.py
# index() method
string = 'Python Strings'
print(string.index('s'))
print(string.index('s', 7, 14))

Output

command
C:\Users\Your Name> python strings.py
7
Traceback (most recent call last):
  File "strings.py", line 4, in <module>
    print(string.index('s', 7, 14))
ValueError: substring not found
command
C:\Users\Your Name> python strings.py
7
Traceback (most recent call last):
  File "strings.py", line 4, in <module>
    print(string.index('s', 7, 14))
ValueError: substring not found

12. isalnum()

The isalnum()isalnum() method returns TrueTrue if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9).

Syntax:

string.isalnum()string.isalnum()

  • Returns - TrueTrue if all characters in the string are alphanumeric

Example:

strings.py
# isalnum() method
string = 'PythonStrings'
print(string.isalnum())
print('Python Strings'.isalnum())
strings.py
# isalnum() method
string = 'PythonStrings'
print(string.isalnum())
print('Python Strings'.isalnum())

Output

command
C:\Users\Your Name> python strings.py
True
False
command
C:\Users\Your Name> python strings.py
True
False

13. isalpha()

The isalpha()isalpha() method returns TrueTrue if all the characters are alphabet letters (a-z).

Syntax:

string.isalpha()string.isalpha()

  • Returns - TrueTrue if all characters in the string are alphabet letters

Example:

strings.py
# isalpha() method
string = 'PythonStrings'
print(string.isalpha())
print('Python Strings'.isalpha())
strings.py
# isalpha() method
string = 'PythonStrings'
print(string.isalpha())
print('Python Strings'.isalpha())

Output

command
C:\Users\Your Name> python strings.py
True
False
command
C:\Users\Your Name> python strings.py
True
False

14. isdecimal()

The isdecimal()isdecimal() method returns TrueTrue if all the characters are decimals (0-9).

Syntax:

string.isdecimal()string.isdecimal()

  • Returns - TrueTrue if all characters in the string are decimals

Example:

strings.py
# isdecimal() method
string = '123456'
print(string.isdecimal())
print('123 456'.isdecimal())
strings.py
# isdecimal() method
string = '123456'
print(string.isdecimal())
print('123 456'.isdecimal())

Output

command
C:\Users\Your Name> python strings.py
True
False
command
C:\Users\Your Name> python strings.py
True
False

15. isdigit()

The isdigit()isdigit() method returns TrueTrue if all the characters are digits, otherwise FalseFalse.

Syntax:

string.isdigit()string.isdigit()

  • Returns - TrueTrue if all characters in the string are digits

Example:

strings.py
# isdigit() method
string = '123456'
print(string.isdigit())
print('123 456'.isdigit())
strings.py
# isdigit() method
string = '123456'
print(string.isdigit())
print('123 456'.isdigit())

Output

command
C:\Users\Your Name> python strings.py
True
False
command
C:\Users\Your Name> python strings.py
True
False

16. isidentifier()

The isidentifier()isidentifier() method returns TrueTrue if the string is a valid identifier, otherwise FalseFalse.

Syntax:

string.isidentifier()string.isidentifier()

  • Returns - TrueTrue if the string is a valid identifier, otherwise FalseFalse

Example:

strings.py
# isidentifier() method
string = 'PythonStrings'
print(string.isidentifier())
print('Python Strings'.isidentifier())
print('Python-Strings'.isidentifier())
print('Python_Strings'.isidentifier())
strings.py
# isidentifier() method
string = 'PythonStrings'
print(string.isidentifier())
print('Python Strings'.isidentifier())
print('Python-Strings'.isidentifier())
print('Python_Strings'.isidentifier())

Output

command
C:\Users\Your Name> python strings.py
True
False
False
True
command
C:\Users\Your Name> python strings.py
True
False
False
True

17. islower()

The islower()islower() method returns TrueTrue if all the characters are in lower case, otherwise FalseFalse.

Syntax:

string.islower()string.islower()

  • Returns - TrueTrue if all characters in the string are lower case

Example:

strings.py
# islower() method
string = 'python strings'
print(string.islower())
print('Python Strings'.islower())
strings.py
# islower() method
string = 'python strings'
print(string.islower())
print('Python Strings'.islower())

Output

command
C:\Users\Your Name> python strings.py
True
False
command
C:\Users\Your Name> python strings.py
True
False

18. isnumeric()

The isnumeric()isnumeric() method returns TrueTrue if all the characters are numeric (0-9), otherwise FalseFalse.

Syntax:

string.isnumeric()string.isnumeric()

  • Returns - TrueTrue if all characters in the string are numeric

Example:

strings.py
# isnumeric() method
string = '123456'
print(string.isnumeric())
print('123 456'.isnumeric())
strings.py
# isnumeric() method
string = '123456'
print(string.isnumeric())
print('123 456'.isnumeric())

Output

command
C:\Users\Your Name> python strings.py
True
False
command
C:\Users\Your Name> python strings.py
True
False

19. isprintable()

The isprintable()isprintable() method returns TrueTrue if all the characters are printable, otherwise FalseFalse.

Syntax:

string.isprintable()string.isprintable()

  • Returns - TrueTrue if all characters in the string are printable

Example:

strings.py
# isprintable() method
string = 'Python Strings'
print(string.isprintable())
print('Python\nStrings'.isprintable())
strings.py
# isprintable() method
string = 'Python Strings'
print(string.isprintable())
print('Python\nStrings'.isprintable())

Output

command
C:\Users\Your Name> python strings.py
True
False
command
C:\Users\Your Name> python strings.py
True
False

20. isspace()

The isspace()isspace() method returns TrueTrue if all the characters in a string are whitespaces, otherwise FalseFalse.

Syntax:

string.isspace()string.isspace()

  • Returns - TrueTrue if all characters in the string are whitespaces

Example:

strings.py
# isspace() method
string = '   '
print(string.isspace())
print('Python Strings'.isspace())
strings.py
# isspace() method
string = '   '
print(string.isspace())
print('Python Strings'.isspace())

Output

command
C:\Users\Your Name> python strings.py
True
False
command
C:\Users\Your Name> python strings.py
True
False

21. istitle()

The istitle()istitle() method returns TrueTrue if all words in a text start with a upper case letter, AND the rest of the word are lower case letters, otherwise FalseFalse.

Syntax:

string.istitle()string.istitle()

  • Returns - TrueTrue if all words in a text start with a upper case letter, AND the rest of the word are lower case letters

Example:

strings.py
# istitle() method
string = 'Python Strings'
print(string.istitle())
print('Python strings'.istitle())
strings.py
# istitle() method
string = 'Python Strings'
print(string.istitle())
print('Python strings'.istitle())

Output

command
C:\Users\Your Name> python strings.py
True
False
command
C:\Users\Your Name> python strings.py
True
False

22. isupper()

The isupper()isupper() method returns TrueTrue if all the characters are in upper case, otherwise FalseFalse.

Syntax:

string.isupper()string.isupper()

  • Returns - TrueTrue if all characters in the string are upper case

Example:

strings.py
# isupper() method
string = 'PYTHON STRINGS'
print(string.isupper())
print('Python Strings'.isupper())
strings.py
# isupper() method
string = 'PYTHON STRINGS'
print(string.isupper())
print('Python Strings'.isupper())

Output

command
C:\Users\Your Name> python strings.py
True
False
command
C:\Users\Your Name> python strings.py
True
False

23. join()

The join()join() method takes all items in an iterable and joins them into one string.

Syntax:

string.join(iterable)string.join(iterable)

  • iterable - Required. Any iterable object where all the returned values are strings

Example:

strings.py
# join() method
string = 'Python Strings'
print(' '.join(string))
print(''.join(string))
strings.py
# join() method
string = 'Python Strings'
print(' '.join(string))
print(''.join(string))

Output

command
C:\Users\Your Name> python strings.py
P y t h o n   S t r i n g s
Python Strings
command
C:\Users\Your Name> python strings.py
P y t h o n   S t r i n g s
Python Strings

24. ljust()

The ljust()ljust() method will left align the string, using a specified character (space is default) as the fill character.

Syntax:

string.ljust(length, character)string.ljust(length, character)

  • length - The length of the returned string
  • character (optional) - The character to fill the missing space on the right side. Default is " "" "
  • Returns - A left aligned string

Example:

strings.py
# ljust() method
string = 'Python Strings'
print(string.ljust(20))
print(string.ljust(20, '*'))
strings.py
# ljust() method
string = 'Python Strings'
print(string.ljust(20))
print(string.ljust(20, '*'))

Output

command
C:\Users\Your Name> python strings.py
Python Strings
Python Strings*******
command
C:\Users\Your Name> python strings.py
Python Strings
Python Strings*******

25. lower()

The lower()lower() method returns a string where all characters are lower case.

Syntax:

string.lower()string.lower()

  • Returns - A lower case string

Example:

strings.py
# lower() method
string = 'Python Strings'
print(string.lower())
strings.py
# lower() method
string = 'Python Strings'
print(string.lower())

Output

command
C:\Users\Your Name> python strings.py
python strings
command
C:\Users\Your Name> python strings.py
python strings

26. lstrip()

The lstrip()lstrip() method removes any leading characters (space is the default leading character to remove)

Syntax:

string.lstrip(characters)string.lstrip(characters)

  • characters (optional) - A set of characters to remove as leading characters
  • Returns - A left trim version of the string

Example:

strings.py
# lstrip() method
string = '   Python Strings'
print(string.lstrip())
print(string.lstrip('   '))
strings.py
# lstrip() method
string = '   Python Strings'
print(string.lstrip())
print(string.lstrip('   '))

Output

command
C:\Users\Your Name> python strings.py
Python Strings
Python Strings
command
C:\Users\Your Name> python strings.py
Python Strings
Python Strings

27. maketrans()

The maketrans()maketrans() method generates a translation table that can be used for replacing specified characters. This method is often used in conjunction with the translate()translate() method to perform character substitutions in a string.

Syntax:

str.maketrans(x[, y[, z]])str.maketrans(x[, y[, z]])

  • x - If only one argument is provided, it should be a dictionary mapping Unicode ordinals to translation strings. If two arguments are provided, they must be of equal length, and each character in x will be mapped to the character at the same position in y. If three arguments are provided, each charac ter in x will be mapped to the character at the same position in y and z.
  • y - Mapping table, where each character in x will be mapped to the character at the same position in y. This argument is optional.
  • z - If present, it specifies a string to be used for deleting characters.

Returns:

  • A translation table.

Example:

strings.py
# maketrans() method
intab = "aeiou"
outtab = "12345"
trans_table = str.maketrans(intab, outtab)
 
string = "Hello, World!"
translated_string = string.translate(trans_table)
 
print("Original String:", string)
print("Translated String:", translated_string)
strings.py
# maketrans() method
intab = "aeiou"
outtab = "12345"
trans_table = str.maketrans(intab, outtab)
 
string = "Hello, World!"
translated_string = string.translate(trans_table)
 
print("Original String:", string)
print("Translated String:", translated_string)

Output

command
C:\Users\Your Name> python strings.py
Original String: Hello, World!
Translated String: H2ll4, W4rld!
command
C:\Users\Your Name> python strings.py
Original String: Hello, World!
Translated String: H2ll4, W4rld!

28. partition()

The partition()partition() method divides a string into three parts based on the specified separator. It searches for the separator in the string, and once found, it returns a tuple containing the part before the separator, the separator itself, and the part after the separator.

Syntax:

string.partition(separator)string.partition(separator)

  • separator - The string to search for within the given string.

Returns:

  • A tuple containing three elements: the part before the separator, the separator itself, and the part after the separator.

Example:

strings.py
# partition() method
string = "Python is fun"
partitioned = string.partition("is")
 
print("Original String:", string)
print("Partitioned Result:", partitioned)
strings.py
# partition() method
string = "Python is fun"
partitioned = string.partition("is")
 
print("Original String:", string)
print("Partitioned Result:", partitioned)

Output

command
C:\Users\Your Name> python strings.py
Original String: Python is fun
Partitioned Result: ('Python ', 'is', ' fun')
command
C:\Users\Your Name> python strings.py
Original String: Python is fun
Partitioned Result: ('Python ', 'is', ' fun')

29. replace()

The replace()replace() method returns a copy of the string where all occurrences of a substring is replaced with another substring.

Syntax:

string.replace(old, new, count)string.replace(old, new, count)

  • old - The substring to be replaced.
  • new - The string which would replace the substring passed.
  • count (optional) - The number of times old substring needs to be replaced with new substring. Default is -1-1 which means replace all occurrences.
  • Returns - A copy of the string where all occurrences of a substring is replaced with another substring.

Example:

strings.py
# replace() method
string = "Python is fun"
replaced = string.replace("is", "was")
print("Original String:", string)
print("Replaced String:", replaced)
strings.py
# replace() method
string = "Python is fun"
replaced = string.replace("is", "was")
print("Original String:", string)
print("Replaced String:", replaced)

Output

command
C:\Users\Your Name> python strings.py
Original String: Python is fun
Replaced String: Python was fun
command
C:\Users\Your Name> python strings.py
Original String: Python is fun
Replaced String: Python was fun

30. rfind()

The rfind()rfind() method finds the last occurrence of the specified value. The rfind()rfind() method returns -1-1 if the value is not found.

Syntax:

string.rfind(value, start, end)string.rfind(value, start, end)

  • value - Required. The value to search for
  • start (optional) - Optional. Where to start the search. Default is 00
  • end (optional) - Optional. Where to end the search. Default is len(string)len(string)
  • Returns - The index of the last occurrence of the specified value

Example:

strings.py
# rfind() method
string = 'Python Strings'
print(string.rfind('s'))
print(string.rfind('s', 7, 14))
strings.py
# rfind() method
string = 'Python Strings'
print(string.rfind('s'))
print(string.rfind('s', 7, 14))

Output

command
C:\Users\Your Name> python strings.py
13
-1
command
C:\Users\Your Name> python strings.py
13
-1

31. rindex()

The rindex()rindex() method finds the last occurrence of the specified value. The rindex()rindex() method raises an exception if the value is not found.

Syntax:

string.rindex(value, start, end)string.rindex(value, start, end)

  • value - Required. The value to search for
  • start (optional) - Optional. Where to start the search. Default is 00
  • end (optional) - Optional. Where to end the search. Default is len(string)len(string)
  • Returns - The index of the last occurrence of the specified value

Example:

strings.py
# rindex() method
string = 'Python Strings'
print(string.rindex('s'))
print(string.rindex('s', 7, 14))
strings.py
# rindex() method
string = 'Python Strings'
print(string.rindex('s'))
print(string.rindex('s', 7, 14))

Output

command
C:\Users\Your Name> python strings.py
13
Traceback (most recent call last):
  File "strings.py", line 4, in <module>
    print(string.rindex('s', 7, 14))
ValueError: substring not found
command
C:\Users\Your Name> python strings.py
13
Traceback (most recent call last):
  File "strings.py", line 4, in <module>
    print(string.rindex('s', 7, 14))
ValueError: substring not found

32. rjust()

The rjust()rjust() method will right align the string, using a specified character (space is default) as the fill character.

Syntax:

string.rjust(length, character)string.rjust(length, character)

  • length - The length of the returned string
  • character (optional) - The character to fill the missing space on the left side. Default is " "" "
  • Returns - A right aligned string

Example:

strings.py
# rjust() method
string = 'Python Strings'
print(string.rjust(20))
print(string.rjust(20, '*'))
strings.py
# rjust() method
string = 'Python Strings'
print(string.rjust(20))
print(string.rjust(20, '*'))

Output

command
C:\Users\Your Name> python strings.py
      Python Strings
*******Python Strings
command
C:\Users\Your Name> python strings.py
      Python Strings
*******Python Strings

33. rpartition()

The rpartition()rpartition() method divides a string into three parts based on the specified separator. It searches for the separator in the string, moving from right to left, and once found, it returns a tuple containing the part before the separator, the separator itself, and the part after the separator.

Syntax:

string.rpartition(separator)string.rpartition(separator)

  • separator - The string to search for within the given string.
  • Returns - A tuple containing three elements: the part before the separator, the separator itself, and the part after the separator.

Example:

strings.py
# rpartition() method
string = "Python is fun"
partitioned = string.rpartition("is")
print("Original String:", string)
print("Partitioned Result:", partitioned)
strings.py
# rpartition() method
string = "Python is fun"
partitioned = string.rpartition("is")
print("Original String:", string)
print("Partitioned Result:", partitioned)

Output

command
C:\Users\Your Name> python strings.py
Original String: Python is fun
Partitioned Result: ('Python ', 'is', ' fun')
command
C:\Users\Your Name> python strings.py
Original String: Python is fun
Partitioned Result: ('Python ', 'is', ' fun')

34. rsplit()

The rsplit()rsplit() method splits a string into a list, starting from the right. If no “max” is specified, this method will return the same as the split()split() method.

Syntax:

string.rsplit(separator, maxsplit)string.rsplit(separator, maxsplit)

  • separator (optional) - Specifies the separator to use when splitting the string. By default any whitespace is a separator
  • maxsplit (optional) - Specifies how many splits to do. Default value is -1-1, which is “all occurrences”
  • Returns - A list of strings split at each separator

Example:

strings.py
# rsplit() method
string = 'Python Strings'
print(string.rsplit())
print(string.rsplit(' ', 1))
strings.py
# rsplit() method
string = 'Python Strings'
print(string.rsplit())
print(string.rsplit(' ', 1))

Output

command
C:\Users\Your Name> python strings.py
['Python', 'Strings']
['Python', 'Strings']
command
C:\Users\Your Name> python strings.py
['Python', 'Strings']
['Python', 'Strings']

35. rstrip()

The rstrip()rstrip() method removes any trailing characters (characters at the end a string), space is the default trailing character to remove.

Syntax:

string.rstrip(characters)string.rstrip(characters)

  • characters (optional) - A set of characters to remove as trailing characters
  • Returns - A right trim version of the string

Example:

strings.py
# rstrip() method
string = 'Python Strings   '
print(string.rstrip())
print(string.rstrip('   '))
strings.py
# rstrip() method
string = 'Python Strings   '
print(string.rstrip())
print(string.rstrip('   '))

Output

command
C:\Users\Your Name> python strings.py
Python Strings
Python Strings
command
C:\Users\Your Name> python strings.py
Python Strings
Python Strings

36. split()

The split()split() method splits a string into a list.

Syntax:

string.split(separator, maxsplit)string.split(separator, maxsplit)

  • separator (optional) - Specifies the separator to use when splitting the string. By default any whitespace is a separator
  • maxsplit (optional) - Specifies how many splits to do. Default value is -1-1, which is “all occurrences”
  • Returns - A list of strings split at each separator

Example:

strings.py
# split() method
string = 'Python Strings'
print(string.split())
print(string.split(' ', 1))
strings.py
# split() method
string = 'Python Strings'
print(string.split())
print(string.split(' ', 1))

Output

command
C:\Users\Your Name> python strings.py
['Python', 'Strings']
['Python', 'Strings']
command
C:\Users\Your Name> python strings.py
['Python', 'Strings']
['Python', 'Strings']

37. splitlines()

The splitlines()splitlines() method splits a string into a list. The splitting is done at line breaks.

Syntax:

string.splitlines(keepends)string.splitlines(keepends)

  • keepends (optional) - Specifies if the line breaks should be included (True), or not (False). Default value is FalseFalse
  • Returns - A list of lines in the string

Example:

strings.py
# splitlines() method
string = 'Python\nStrings'
print(string.splitlines())
print(string.splitlines(True))
strings.py
# splitlines() method
string = 'Python\nStrings'
print(string.splitlines())
print(string.splitlines(True))

Output

command
C:\Users\Your Name> python strings.py
['Python', 'Strings']
['Python\n', 'Strings']
command
C:\Users\Your Name> python strings.py
['Python', 'Strings']
['Python\n', 'Strings']

38. startswith()

The startswith()startswith() method returns TrueTrue if the string starts with the specified value, otherwise FalseFalse.

Syntax:

string.startswith(value, start, end)string.startswith(value, start, end)

  • value - Required. The value to check if the string starts with
  • start (optional) - Optional. An Integer specifying at which position to start the search
  • end (optional) - Optional. An Integer specifying at which position to end the search
  • Returns - TrueTrue if the string starts with the specified value, otherwise FalseFalse

Example:

strings.py
# startswith() method
string = 'Python Strings'
print(string.startswith('P'))
print(string.startswith('p', 7, 14))
strings.py
# startswith() method
string = 'Python Strings'
print(string.startswith('P'))
print(string.startswith('p', 7, 14))

Output

command
C:\Users\Your Name> python strings.py
True
False
command
C:\Users\Your Name> python strings.py
True
False

39. strip()

The strip()strip() method removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default leading character to remove)

Syntax:

string.strip(characters)string.strip(characters)

  • characters (optional) - A set of characters to remove as leading/trailing characters
  • Returns - A trimmed version of the string

Example:

strings.py
# strip() method
string = '   Python Strings   '
print(string.strip())
print(string.strip('   '))
strings.py
# strip() method
string = '   Python Strings   '
print(string.strip())
print(string.strip('   '))

Output

command
C:\Users\Your Name> python strings.py
Python Strings
Python Strings
command
C:\Users\Your Name> python strings.py
Python Strings
Python Strings

40. swapcase()

The swapcase()swapcase() method returns a string where all the upper case letters are lower case and vice versa.

Syntax:

string.swapcase()string.swapcase()

  • Returns - A string where all the upper case letters are lower case and vice versa

Example:

strings.py
# swapcase() method
string = 'Python Strings'
print(string.swapcase())
strings.py
# swapcase() method
string = 'Python Strings'
print(string.swapcase())

Output

command
C:\Users\Your Name> python strings.py
pYTHON sTRINGS
command
C:\Users\Your Name> python strings.py
pYTHON sTRINGS

41. title()

The title()title() method returns a string where the first character in every word is upper case. Like a header, or a title.

Syntax:

string.title()string.title()

  • Returns - A string where the first character in every word is upper case

Example:

strings.py
# title() method
string = 'Python Strings'
print(string.title())
strings.py
# title() method
string = 'Python Strings'
print(string.title())

Output

command
C:\Users\Your Name> python strings.py
Python Strings
command
C:\Users\Your Name> python strings.py
Python Strings

42. translate()

The translate()translate() method returns a string where some specified characters are replaced with the character described in a dictionary, or in a mapping table.

Syntax:

string.translate(table)string.translate(table)

  • table - A mapping table, where each character in the intab parameter will be mapped to the character at the same position in the outtab parameter.
  • Returns - A string where specified characters are replaced with specified characters

Example:

strings.py
# translate() method
string = 'Python Strings'
print(string.translate({ord('P'): 'J'}))
print(string.translate({ord('P'): 'J', ord('S'): 'L'}))
strings.py
# translate() method
string = 'Python Strings'
print(string.translate({ord('P'): 'J'}))
print(string.translate({ord('P'): 'J', ord('S'): 'L'}))

Output

command
C:\Users\Your Name> python strings.py
Jython Strings
Jython Ltrings
command
C:\Users\Your Name> python strings.py
Jython Strings
Jython Ltrings

43. upper()

The upper()upper() method returns a string where all characters are in upper case.

Syntax:

string.upper()string.upper()

  • Returns - A string where all characters are in upper case
  • Example:
strings.py
# upper() method
string = 'Python Strings'
print(string.upper())
strings.py
# upper() method
string = 'Python Strings'
print(string.upper())

Output

command
C:\Users\Your Name> python strings.py
PYTHON STRINGS
command
C:\Users\Your Name> python strings.py
PYTHON STRINGS

44. zfill()

The zfill()zfill() method adds zeros (0) at the beginning of the string, until it reaches the specified length.

Syntax:

string.zfill(length)string.zfill(length)

  • length - The length of the returned string, with 00 digits filled to the left
  • Returns - A copy of the string with 00 digits to the left of the specified length

Example:

strings.py
# zfill() method
string = 'Python Strings'
print(string.zfill(20))
print(string.zfill(20).upper())
strings.py
# zfill() method
string = 'Python Strings'
print(string.zfill(20))
print(string.zfill(20).upper())

Output

command
C:\Users\Your Name> python strings.py
000000Python Strings
000000PYTHON STRINGS
command
C:\Users\Your Name> python strings.py
000000Python Strings
000000PYTHON STRINGS

45. +

The ++ operator is used to concatenate two strings.

Syntax:

string1 + string2string1 + string2

  • string1 - Required. First string to be concatenated
  • string2 - Required. Second string to be concatenated
  • Returns - A concatenated string

Example:

strings.py
# + operator
string1 = 'Python'
string2 = 'Strings'
print(string1 + string2)
strings.py
# + operator
string1 = 'Python'
string2 = 'Strings'
print(string1 + string2)

Output

command
C:\Users\Your Name> python strings.py
PythonStrings
command
C:\Users\Your Name> python strings.py
PythonStrings

46. *

The ** operator is used to repeat a string for a given number of times.

Syntax:

string * numberstring * number

  • string - Required. The string to be repeated
  • number - Required. A number specifying how many times the string should be repeated
  • Returns - A string repeated the specified number of times

Example:

strings.py
# * operator
string = 'Python Strings'
print(string * 2)
print(string * 3)
strings.py
# * operator
string = 'Python Strings'
print(string * 2)
print(string * 3)

Output

command
C:\Users\Your Name> python strings.py
Python StringsPython Strings
Python StringsPython StringsPython Strings
command
C:\Users\Your Name> python strings.py
Python StringsPython Strings
Python StringsPython StringsPython Strings

47. []

The [][] operator is used to slice a string.

Syntax:

string[index]string[index]

  • index - Required. An integer specifying at which position to start the slicing. The indexing starts from 00
  • Returns - A sliced string

Example:

strings.py
# [] operator
string = 'Python Strings'
print(string[0])
print(string[7])
strings.py
# [] operator
string = 'Python Strings'
print(string[0])
print(string[7])

Output

command
C:\Users\Your Name> python strings.py
P
S
command
C:\Users\Your Name> python strings.py
P
S

48. [:]

The [:][:] operator is used to slice a string.

Syntax:

string[start:end:step]string[start:end:step]

  • start (optional) - Optional. An integer specifying at which position to start the slicing. The indexing starts from 00
  • end (optional) - Optional. An integer specifying at which position to end the slicing
  • step (optional) - Optional. An integer specifying the step of the slicing. Default is 11
  • Returns - A sliced string

Example:

strings.py
# [:] operator
string = 'Python Strings'
print(string[:6])
print(string[7:])
strings.py
# [:] operator
string = 'Python Strings'
print(string[:6])
print(string[7:])

Output

command
C:\Users\Your Name> python strings.py
Python
Strings
command
C:\Users\Your Name> python strings.py
Python
Strings

49. in

The inin operator returns TrueTrue if a specified character is present in the string.

Syntax:

character in stringcharacter in string

  • character - Required. A character to be searched for
  • string - Required. The string to search in
  • Returns - TrueTrue if the specified character is present in the string

Example:

strings.py
# in operator
string = 'Python Strings'
print('P' in string)
print('p' in string)
strings.py
# in operator
string = 'Python Strings'
print('P' in string)
print('p' in string)

Output

command
C:\Users\Your Name> python strings.py
True
False
command
C:\Users\Your Name> python strings.py
True
False

50. not in

The not innot in operator returns TrueTrue if a specified character is not present in the string.

Syntax:

character not in stringcharacter not in string

  • character - Required. A character to be searched for
  • string - Required. The string to search in
  • Returns - TrueTrue if the specified character is not present in the string

Example:

strings.py
# not in operator
string = 'Python Strings'
print('P' not in string)
print('p' not in string)
strings.py
# not in operator
string = 'Python Strings'
print('P' not in string)
print('p' not in string)

Output

command
C:\Users\Your Name> python strings.py
False
True
command
C:\Users\Your Name> python strings.py
False
True

51. %

The %% operator is used to format a set of variables enclosed in a “tuple” (a fixed size list), together with a format string, which contains normal text together with “argument specifiers”, special symbols like %s%s and %d%d.

Syntax:

string % valuesstring % values

  • string - Required. A string containing the format string and argument specifiers
  • values - Required. A tuple containing the values to be formatted
  • Returns - A formatted string

Example:

strings.py
# % operator
string = 'Python %s'
print(string % 'Strings')
print(string % 3.6)
strings.py
# % operator
string = 'Python %s'
print(string % 'Strings')
print(string % 3.6)

Output

command
C:\Users\Your Name> python strings.py
Python Strings
Python 3.6
command
C:\Users\Your Name> python strings.py
Python Strings
Python 3.6

52. <

The << operator is used to compare two strings, to determine if the left string is less than the right string.

Syntax:

string1 < string2string1 < string2

  • string1 - Required. The first string to be compared
  • string2 - Required. The second string to be compared
  • Returns - TrueTrue if the left string is less than the right string

Example:

strings.py
# < operator
string1 = 'Python'
string2 = 'Strings'
print(string1 < string2)
print(string1 < 'Python')
strings.py
# < operator
string1 = 'Python'
string2 = 'Strings'
print(string1 < string2)
print(string1 < 'Python')

Output

command
C:\Users\Your Name> python strings.py
True
False
command
C:\Users\Your Name> python strings.py
True
False

53. <=

The <=<= operator is used to compare two strings, to determine if the left string is less than or equal to the right string.

Syntax:

string1 <= string2string1 <= string2

  • string1 - Required. The first string to be compared
  • string2 - Required. The second string to be compared
  • Returns - TrueTrue if the left string is less than or equal to the right string

Example:

strings.py
# <= operator
string1 = 'Python'
string2 = 'Strings'
print(string1 <= string2)
print(string1 <= 'Python')
strings.py
# <= operator
string1 = 'Python'
string2 = 'Strings'
print(string1 <= string2)
print(string1 <= 'Python')

Output

command
C:\Users\Your Name> python strings.py
True
True
command
C:\Users\Your Name> python strings.py
True
True

54. >

The >> operator is used to compare two strings, to determine if the left string is greater than the right string.

Syntax:

string1 > string2string1 > string2

  • string1 - Required. The first string to be compared
  • string2 - Required. The second string to be compared
  • Returns - TrueTrue if the left string is greater than the right string

Example:

strings.py
# > operator
string1 = 'Python'
string2 = 'Strings'
print(string1 > string2)
print(string1 > 'Python')
strings.py
# > operator
string1 = 'Python'
string2 = 'Strings'
print(string1 > string2)
print(string1 > 'Python')

Output

command
C:\Users\Your Name> python strings.py
False
False
command
C:\Users\Your Name> python strings.py
False
False

55. >=

The >=>= operator is used to compare two strings, to determine if the left string is greater than or equal to the right string.

Syntax:

string1 >= string2string1 >= string2

  • string1 - Required. The first string to be compared
  • string2 - Required. The second string to be compared
  • Returns - TrueTrue if the left string is greater than or equal to the right string

Example:

strings.py
# >= operator
string1 = 'Python'
string2 = 'Strings'
print(string1 >= string2)
print(string1 >= 'Python')
strings.py
# >= operator
string1 = 'Python'
string2 = 'Strings'
print(string1 >= string2)
print(string1 >= 'Python')

Output

command
C:\Users\Your Name> python strings.py
False
True
command
C:\Users\Your Name> python strings.py
False
True

56. ==

The ==== operator is used to compare two strings, to determine if the left string is equal to the right string.

Syntax:

string1 == string2string1 == string2

  • string1 - Required. The first string to be compared
  • string2 - Required. The second string to be compared
  • Returns - TrueTrue if the left string is equal to the right string

Example:

strings.py
# == operator
string1 = 'Python'
string2 = 'Strings'
print(string1 == string2)
print(string1 == 'Python')
strings.py
# == operator
string1 = 'Python'
string2 = 'Strings'
print(string1 == string2)
print(string1 == 'Python')

Output

command
C:\Users\Your Name> python strings.py
False
True
command
C:\Users\Your Name> python strings.py
False
True

57. !=

The !=!= operator is used to compare two strings, to determine if the left string is not equal to the right string.

Syntax:

string1 != string2string1 != string2

  • string1 - Required. The first string to be compared
  • string2 - Required. The second string to be compared
  • Returns - TrueTrue if the left string is not equal to the right string

Example:

strings.py
# != operator
string1 = 'Python'
string2 = 'Strings'
print(string1 != string2)
print(string1 != 'Python')
strings.py
# != operator
string1 = 'Python'
string2 = 'Strings'
print(string1 != string2)
print(string1 != 'Python')

Output

command
C:\Users\Your Name> python strings.py
True
False
command
C:\Users\Your Name> python strings.py
True
False

58. ord()

The ord()ord() function returns an integer representing the Unicode character.

Syntax:

ord(character)ord(character)

  • character - Required. A character
  • Returns - An integer representing the Unicode character

Example:

strings.py
# ord() function
print(ord('A'))
print(ord('a'))
strings.py
# ord() function
print(ord('A'))
print(ord('a'))

Output

command
C:\Users\Your Name> python strings.py
65
97
command
C:\Users\Your Name> python strings.py
65
97

59. hex()

The hex()hex() function converts an integer number to the corresponding hexadecimal string.

Syntax:

hex(number)hex(number)

  • number - Required. An integer number (int object)
  • Returns - A hexadecimal string

Example:

strings.py
# hex() function
print(hex(255))
print(hex(-42))
strings.py
# hex() function
print(hex(255))
print(hex(-42))

Output

command
C:\Users\Your Name> python strings.py
0xff
-0x2a
command
C:\Users\Your Name> python strings.py
0xff
-0x2a

60. oct()

The oct()oct() function converts an integer number to the corresponding octal string.

Syntax:

oct(number)oct(number)

  • number - Required. An integer number (int object)
  • Returns - An octal string

Example:

strings.py
# oct() function
print(oct(255))
print(oct(-42))
strings.py
# oct() function
print(oct(255))
print(oct(-42))

Output

command
C:\Users\Your Name> python strings.py
0o377
-0o52
command
C:\Users\Your Name> python strings.py
0o377
-0o52

61. bin()

The bin()bin() function converts an integer number to the corresponding binary string.

Syntax:

bin(number)bin(number)

  • number - Required. An integer number (int object)
  • Returns - A binary string

Example:

strings.py
# bin() function
print(bin(255))
print(bin(-42))
strings.py
# bin() function
print(bin(255))
print(bin(-42))

Output

command
C:\Users\Your Name> python strings.py
0b11111111
-0b101010
command
C:\Users\Your Name> python strings.py
0b11111111
-0b101010

62. chr()

The chr()chr() function returns a character (a string) from an integer (represents unicode code point of the character).

Syntax:

chr(number)chr(number)

  • number - Required. An integer representing the Unicode code point of the character
  • Returns - A character (a string) from an integer (represents unicode code point of the character)

Example:

strings.py
# chr() function
print(chr(65))
print(chr(97))
strings.py
# chr() function
print(chr(65))
print(chr(97))

Output

command
C:\Users\Your Name> python strings.py
A
a
command
C:\Users\Your Name> python strings.py
A
a

63. len()

The len()len() function returns the number of items (length) in an object.

Syntax:

len(object)len(object)

  • object - Required. An object (string, bytes or array etc.)
  • Returns - The number of items in an object

Example:

strings.py
# len() function
print(len('Python'))
print(len('Python Strings'))
strings.py
# len() function
print(len('Python'))
print(len('Python Strings'))

Output

command
C:\Users\Your Name> python strings.py
6
14
command
C:\Users\Your Name> python strings.py
6
14

64. repr()

The repr()repr() function returns a printable representation of the given object.

Syntax:

repr(object)repr(object)

  • object - Required. Any object, like lists, tuples, strings etc.
  • Returns - A printable representation of the given object

Example:

strings.py
# repr() function
print(repr('Python'))
print(repr('Python Strings'))
strings.py
# repr() function
print(repr('Python'))
print(repr('Python Strings'))

Output

command
C:\Users\Your Name> python strings.py
'Python'
'Python Strings'
command
C:\Users\Your Name> python strings.py
'Python'
'Python Strings'

65. ascii()

The ascii()ascii() function returns a readable version of any object (Strings, Tuples, Lists, etc).

Syntax:

ascii(object)ascii(object)

  • object - Required. Any object, like lists, tuples, strings etc.
  • Returns - A readable version of any object (Strings, Tuples, Lists, etc)

Example:

strings.py
# ascii() function
print(ascii('Python'))
print(ascii('Python Strings'))
strings.py
# ascii() function
print(ascii('Python'))
print(ascii('Python Strings'))

Output

command
C:\Users\Your Name> python strings.py
'Python'
'Python Strings'
command
C:\Users\Your Name> python strings.py
'Python'
'Python Strings'

66. max()

The max()max() function returns the largest item in an iterable.

Syntax:

max(iterable)max(iterable)

  • iterable - Required. An iterable object (list, tuple, string etc.)
  • Returns - The largest item in the given iterable

Example:

strings.py
# max() function
print(max('Python'))
print(max('Python Strings'))
strings.py
# max() function
print(max('Python'))
print(max('Python Strings'))

Output

command
C:\Users\Your Name> python strings.py
y
y
command
C:\Users\Your Name> python strings.py
y
y

67. min()

The min()min() function returns the smallest item in an iterable.

Syntax:

min(iterable)min(iterable)

  • iterable - Required. An iterable object (list, tuple, string etc.)
  • Returns - The smallest item in the given iterable

Example:

strings.py
# min() function
print(min('Python'))
print(min('Python Strings'))
strings.py
# min() function
print(min('Python'))
print(min('Python Strings'))

Output

command
C:\Users\Your Name> python strings.py
P
P
command
C:\Users\Your Name> python strings.py
P
P

68. str()

The str()str() function returns the string version of the given object.

Syntax:

str(object)str(object)

  • object - Required. An object to be converted to string
  • Returns - The string version of the given object

Example:

strings.py
# str() function
print(str(3.6))
print(str(3.6) + ' is a float number')
strings.py
# str() function
print(str(3.6))
print(str(3.6) + ' is a float number')

Output

command
C:\Users\Your Name> python strings.py
3.6
3.6 is a float number
command
C:\Users\Your Name> python strings.py
3.6
3.6 is a float number

69. type()

The type()type() function returns the type of the specified object.

Syntax:

type(object)type(object)

  • object - Required. An object whose type needs to be returned
  • Returns - The type of the specified object

Example:

strings.py
# type() function
print(type('Python'))
print(type(3.6))
strings.py
# type() function
print(type('Python'))
print(type(3.6))

Output

command
C:\Users\Your Name> python strings.py
<class 'str'>
<class 'float'>
command
C:\Users\Your Name> python strings.py
<class 'str'>
<class 'float'>

70. help()

The help()help() function is used to display the documentation of modules, functions, classes, keywords etc.

Syntax:

help(object)help(object)

  • object - Required. The object to be described
  • Returns - The documentation of the specified object

Example:

strings.py
# help() function
print(help(str.upper))
strings.py
# help() function
print(help(str.upper))

Output

command
C:\Users\Your Name> python strings.py
Help on method_descriptor:
 
upper(self, /)
    Return a copy of the string converted to uppercase.
 
None
command
C:\Users\Your Name> python strings.py
Help on method_descriptor:
 
upper(self, /)
    Return a copy of the string converted to uppercase.
 
None

Conclusion

In this tutorial, we have learned about the Python string methods and operators with the help of examples. We have also learned about the built-in functions that can be used with strings. Now you can use these methods and operators to manipulate strings in your Python programs.

Was this page helpful?

Let us know how we did