Study Korner

Python Chapter#7: Python Strings

Python Strings

In Python character type variables are not defined; they are treated as strings of length one.

Ex:

str = "Hello world!"
print(str)          # Prints complete string
print(str[0])       # Prints 1st character of the string
print(str[2:5])     # Prints 3rd to 5th characters of the str
print(str[2:])      # Prints rest of the string from 3rd character
print(str * 2)      # Prints string twice
print(str + "TEST") # Prints concatenated string

OUTPUT:

Hello world!
H
llo
llo world!
Hello world!Hello world!
Hello world!TEST
Triple Quotes

Row strings are defined by enclosing string within triple, single quotes or double quotes. While printing the tabs, new lines are also displayed. Ex:

para_str = ""This shows how triple quotes been executed.
It shows how multiple lines and non-printable symbols such as
TAB (\t), NEWLINEs [\n]are displayed."""
print (para_str)

OUTPUT:

This shows how triple quotes been executed.
It shows how multiple lines and non-printable symbols such as
TAB (	), NEWLINEs [
]are displayed.
Built-in String Methods

Python includes the following built-in methods/functions to manipulate strings:

Method/Function Description/Example
len(string) Returns the length of the string
find(str, beg, end) Determine if str occurs in string or in a substring of string if starting index beg and ending index end are given returns index if found and -1 otherwise
rfind(str, beg, end) Same as find(), but search backwards in string
index(str, beg, end) Same as find(), but raises an exception if str not found.
rindex( str, beg, end) Same as index(), but search backwards in string
max(str) Returns the max alphabetical character from the string str.
min(str) Returns the min alphabetical character from the string str.
startswith(str, beg, end) Determines if string or a substring of string (if starting index beg and ending index end are given) starts with substring str; returns true if so and false otherwise
endswith(suffix, beg, end) Determines if string or a substring of string (if starting index beg and ending index end are given) ends with suffix; returns true if so and false otherwise.
join(seq) Merges (concatenates) the string representations of elements in sequence seq into a string, with separator string.
count(str, beg, end) Counts how many times str occurs in string or in a substring of string if starting index beg and ending index end are given.
capitalize() Capitalizes first letter of string
swapcase() Inverts case for all letters in string.
lower() Converts all uppercase letters in string to lowercase.
upper() Converts lowercase letters in string to uppercase.
split(“str”, num) Splits string according to delimiter str (space if not provided) into at most num substrings and returns all the of them.
splitlines(num) Splits string at all (or num) NEWLINEs and returns a list of each line with NEWLINEs removed.
strip() Performs both lstrip() and rstrip() on string
lstrip() Removes all leading white space in string.
rstrip() Removes all trailing white space of string.
replace(old, new, max) It replaces all occurrences of old in string with new or at most max occurrences if max given. Ex: s=”wscdc” s.replace(“c”,”a”,1) print(s) will return wsadc
ljust(width, fillchar) Returns a string padded with fillchar and left-justified to a total of width columns.
rjust(width, fillchar) Returns a string padded with fillchar and right-justified to a total of width columns.
zfill (width) Returns a string left padded with zeros to a total of width characters; intended for numbers, zfill() retains any sign given (less one zero).
title() Returns the original string with all words begin with uppercase and the rest are lowercase.
center(width, fillchar) Returns a fillchar-padded string with a total of width columns
isalnum() Returns true if string has at least 1 character and all characters are alphanumeric and false otherwise.
isalpha() Returns true if string has at least 1 character and all characters are alphabetic and false otherwise.
isdigit() Returns true if string contains only digits and false otherwise
islower() Returns true if string has at least 1 cased character and all cased characters are in lowercase and false otherwise
isnumeric() Returns true if a unicode string contains only numeric characters and false otherwise
isdecimal() Returns true if a unicode string contains only decimal characters and false otherwise
isspace() Returns true if string contains only whitespace characters and false otherwise.
istitle() Returns true if string is properly “titlecased” and false otherwise.
isupper() Returns true if string has at least one cased character and all cased characters are in uppercase and false otherwise.

Exit mobile version