Strings in Python

Strings are a sequence of characters used to store text. For example if you want to store the name of a person, or address, then string is the most appropriate.

Strings in Python can be enclosed in single quotes or double quotes.

Looping

Strings are iterable objects. The standard loops ('for' and 'while') can be used to iterate over a string. The 'for' loop helps to iterate through each character by storing the character in some variable.

str = input("Enter a string: ")
for i in str:
 print('Character: ' + i)

String Operators

Python provides a wide variety of extremely useful operators to manipulate strings.

The Concatenation Operator (+)

The concatenation operator takes two strings and produces a concatenated string. The operator acts on values as well as variables.

Functions for Handling Strings

len()

The function returns the number of characters in a string. The space is also taken into account while calculating the length of the string. The function takes a string argument and returns an integer, which is the length of the string.

length = len(str)
print('Total Characters ', length)

capitalize() and title()

The function capitalizes the first character of the string. If you wants to capitalize the first characters of all the words in the string, the title() function can be used.

print(str.capitalize())
print(str.title())

find()

The find() is used to find the location of a given sub-string in a string.

count()

The number of occurrences of a particular sub-string can be found with the count() function.

endswith()

One can determine if a string ends with a particular sub-string. This can be done using the endswith() function. The function returns a 'True' if the given string ends with the given sub-string, otherwise it returns a 'False'.

encode() and decode()

The encode() function is used to encode a given string in various formats.

Other Functions

  • isanum()
  • isalpha()
  • isdecimal()
  • isdigit()
  • isidentifier()
  • islower()
  • isupper()
  • swapcase()
  • isspace()
  • lstrip()
  • rstrip()
  • replace()
  • join()

The contents of a given string can be checked using the following functions. The isalnum() function if the given string is alphanumeric. The other functions like isalpha() and isdecimal() also check the type of contents in a given string. Whether a given string contains only digits can be checked using the isdigit() function. Similarly, whether a given string is an identifier can be checked using the isidentifier() function.

The islower() function checks if the given string contains only lower case characters and the isupper() function checks if the given string contains only upper case characters. The swapcase() function swaps the case of the given string, as in converts the upper case to lower and lower to upper. The presence of spaces (only) can be checked using the isspace() function.

Extra spaces can be removed from the left and the right hand sides by using the lstrip() and rstrip() functions. The replace() function replaces the instances of the first argument by the string in the second argument. The split() function splits the given strings into tokens. The function of join() is exactly the opposite as that of split.