Python Strings
A String means a sequence of characters. Python strings are always surrounded by single quote(') or double qoute (") even triple quote(''') characters. such as 'hello' or "hello" or '''hello'''. By the quotation mark Python knows where the string begins and ends.
Example:
my_string1 = "Hello World!"
my_string2 = 'Hello World!'
my_string3 = '''Hello World!'''
my_string4 = """Hello World!"""
print(my_string1)
print(my_string2)
print(my_string3)
print(my_string4)
Output:
Hello World! Hello World! Hello World! Hello World!
Indexing and Slicing of a String
You can access the character of a string using the bracket. The expression in brackets is called an index. In python the index always starts with 0. If you specify an index you will get the character at that position in the string.
Example:
country = "Bangladesh"
letter = country[1]
print(letter)
Output:
a
The example extracts the character at index position 1 from the country variable and assigns it to the letter variable. As the index starts with 0 the output of the example is a.
You can also specify a range from one index to another.
Example:
country = "Bangladesh"
letter = country[2:5]
print(letter)
Output:
ngl
Negative indexing can also be used. It starts from the last value. To get the characters from 5 to 1 position you should start the count from the end of the string.
Example:
country = "Bangladesh"
letter = country[-5:-2]
print(letter)
Output:
ade
Length of a String
To get length of a string there is a built-in function in Python. len() is the built-in function that returns the number of character in a string.
Example:
country = "Bangladesh"
length = len(country)
print(length)
Output:
10
Useful String Methods
Python has a set of built-in methods. Here some useful methods are described:
upper(): This method returns a new string where all the letter in the original string have been converted to uppercase.
Example:
country = "Bangladesh"
country = country.upper()
print(country)
Output:
BANGLADESH
lower(): This method returns a new string where all the letter in the original string have been converted to lowercase.
Example:
country = "Bangladesh"
country = country.lower()
print(country)
Output:
bangladesh
strip(): This method removes whitespace from the beginning or the end.
Example:
country = " Bangladesh "
country = country.strip()
print(country)
Output:
Bangladesh
replace(): This method removes whitespace from the beginning or the end.
Example:
mystring = "Rahim"
mystring = mystring.replace("R","F")
print(mystring)
Output:
Fahim
split(): This method removes whitespace from the beginning or the end.
Example:
mystring = "I love my country"
mystring = mystring.split(",")
print(mystring)
Output:
['I', ' love', ' my', ' country']
String Methods
There are a set of built-in methods in python which you can use on strings.
Method | Description |
---|---|
capitalize() | Converts first character to Capital Letter |
center() | Returns a centered string |
casefold() | Converts to casefolded strings |
count() | Returns occurrences of substring in string |
endswith() | Checks if String Ends with the Specified Suffix |
expandtabs() | Replaces Tab character With Spaces |
encode() | Returns encoded string of given string |
find() | Returns the index of first occurrence of substring |
format() | formats string into precise output |
index() | Returns Index of Substring |
isalnum() | Checks Alphanumeric Character |
isalpha() | Checks if All Characters are Alphabets |
isdecimal() | Checks Decimal Characters |
isdigit() | Checks Digit Characters |
isidentifier() | Checks for Valid Identifier |
islower() | Checks if all Alphabets in a String are Lowercase |
isnumeric() | Checks Numeric Characters |
isprintable() | Checks Printable Character |
isspace() | Checks Whitespace Characters |
istitle() | Checks for Titlecased String |
isupper() | returns if all characters are uppercase characters |
join() | Returns a Concatenated String |
ljust() | returns left-justified string of given width |
rjust() | returns right-justified string of given width |
lower() | returns lowercased string |
upper() | returns uppercased string |
swapcase() | swap uppercase characters to lowercase; vice versa |
lstrip() | Removes Leading Characters |
rstrip() | Removes Trailing Characters |
strip() | Removes Both Leading and Trailing Characters |
replace() | Replaces Substring Inside |
rfind() | Returns the Highest Index of Substring |
rindex() | Returns Highest Index of Substring |
startswith() | Returns true if the string starts with the specified value |
title() | Converts the first character of each word to upper case |
zfill() | Fills the string with a specified number of 0 values at the beginning |
translate() | returns mapped charactered string |