Learn About Strings In Python

Introduction

  • String is a sequence of characters and each character can be individually accessed using an Index.
  • Strings are stored as individual characters in a contiguous location, with a 2-way index for each location.
  • We can create them simply by enclosing the characters in quotes (eg:- ‘Hello’).
Note
 
Python treats single quotes the same as double-quotes.
 
Strings In Python
 
You cannot change the individual letters of a string in place by assignment because strings are immutable and hence item assignment is not supported.
 
Strings In Python
 

Traversing a string

 
(Iteration through elements of a string, one character at a time)
 
As mentioned above, individual characters of a string are accessible through a unique index of each character. Using these indexes, you can traverse a string character-by-character.
 
Strings In Python
 
Using range()
 
Strings In Python
 

STRING OPERATORS

 
Strings In Python
 

String Concatenation

 
The + operator creates a new string by joining two operand strings.
 
Strings In Python
 
Some Examples
 
Strings In Python
 
NOTE
 
+ operator can work with Numbers and strings separately for Addition and Concatenation respectively; but in the same expression, you cannot combine numbers and strings as operand with + operator.
 
Strings In Python
 

String Replication

  • The * operator when used with numbers, performs Multiplication and returns the product of two numbers.
  • To use an * operator in Strings, one operand must be a String and one must be a number.
Syntax
 
number*string or string*number
 
Strings In Python
 
String operand tells the string to be replicated and number operand tells the number of times it is to be repeated.
 
Some Examples
Strings In Python
 
NOTE
 
You cannot have strings as both the operands with * operator.
 
Strings In Python
 

IN and NOT IN

 
There are two membership operators IN and NOT IN.
  • IN- returns True if a character or a substring exists in the given string; False otherwise.
  • NOT IN- returns True if a character or a substring does not exist in a given string; False otherwise.
Some Examples
 
Strings In Python
 

Comparison Operators

  • The comparison or relational operators in Python are: >, >=, <, <=, !=
  • The comparison using these operators is based on the standard character-by-character comparison rule for Unicode.
  • For most common characters ASCII values and Unicode values are same.
Strings In Python
 
Some Examples
 
Strings In Python
 

STRING METHODS

 
Strings In Python
 
string.isalnum()
 
This method returns True if the characters in the string are alphanumeric and there is at least one character; otherwise False.
 
Strings In Python
 
string.isalpha()
 
This method returns  True if the characters in the string are alphabetic and there must be at least one character; otherwise False.
 
Strings In Python
 
string.isdigit()
 
This method returns  True if the characters in the string are digits and there must be at least one character; otherwise False.
 
Strings In Python
 
string.islower()
 
This method returns True if all cased characters in the string are lowercase and there must be at least one cased character; otherwise False.
 
Strings In Python
 
string.isupper()
 
This method returns  True if all cased characters in the string are uppercase and there must be at least one cased character; otherwise False.
 
Strings In Python
 
string.isspace()
 
This method returns True if there are only whitespace characters in the string and there must be at least one character; otherwise False.
 
Strings In Python
 
string.capitalize()
 
This method returns a copy of the string with its first character capitalized.
 
Strings In Python
 
string.find(sub[,start[,end]])
  • This method returns the lowest index in the string where the substring is found within the slice range of start and end.
  • Returns -1 if a sub is not found.
Strings In Python
 
string.upper()
 
This method returns a copy of the string converted to uppercase.
 
Strings In Python
 
string.lower()
 
This method returns a copy of the string converted to lowercase.
 
Strings In Python
 

Summary

 
In this article, we discussed all the String methods and operators. I hope this will help the readers to understand how to use and implement Strings in Python.
 
Feedback or queries related to this article are most welcome.
 
Thanks for reading.


Similar Articles