Python – Strings:

In this tutorial, we will work on the Python Strings where we can learn about the manipulation of Strings, using String Operators and string methods and Functions.

First, let us understand that how do we declare the strings in python programming language.

We can declare and print the strings by placing them in single Quotes (‘..’), Double Quotes (“..”), and using the print function too.

Python Strings are Immutable (An Object with a fixed value).

Using the Strings in single quotes (‘…’)

Below command will give an error.

To overcome that we must use escape character \

Using the Strings in Double quotes (“…”)

Using the Strings in Print() function

Using the 3 double quotes start and end of the string allows us to print the data including spaces and newlines.

String Concatenation:

Multiple Strings can be concatenated using (+) symbol. Let us see the example of concatenating the strings.

Example:

String Repetition:

String repetition can be performed by using the (*) symbol. Let us see the example of repetition of strings.

Example:

Strings are indexed with each character in a memory location when assigned to a variable. The indexed number starts from zero ‘0’ from first character till the end of the string. Whereas, reverse indexing starts with ‘-1’ from right to left until the starting character. Let us try few examples of retrieving the characters from a word PYTHON in either ways.

Example:

String Methods in Python:

Python String Methods Description
capitalize() Returns the String with first Character as Capital Letter
casefold() Returns a casefolded copy
center(width[, fillchar]) This will pads the string with a character specified
count(sub[, start[, end]]) Returns the number of occurances of substring in string
encode(encoding=”utf-8″, errors=”strict”) returns an encoded string
endswith(suffix[, start[, end]]) Check the string if it ends with the specified
expandtabs(tabsize=8) Replace the tab with space
find(sub[, start[, end]]) returns the highest index
format(*args, **kwargs) formats the string
format_map(mapping) formats the string except the mapping is directly used
index(sub[, start[, end]]) returns the index of substring
isalnum() checks for alphanumeric Char
isalpha() Checks if all characters are Alphabets
isdecimal() Checks for decimal characters
isdigit() Checks for digit char
isidentifier() checks for valid Identifier
islower() checks for lowercase of all alphabets in string
isnumeric() Checks for Numeric Char
isprintable() Checks for Printable Char
isspace() Checks for Whitespace Characters
istitle() Returns true if the string is titlecased
isupper() Checks if all characters are Uppercase
join(iterable) returns concatenated string
ljust(width[, fillchar]) returns left-justified string
lower() returns lowercased string
lstrip([chars]) Removes Leading Characters
partition(sep) returns a tuple
replace(old, new[, count]) replaces the substring
rfind(sub[, start[, end]]) Returns the Highest Index
rindex(sub[, start[, end]]) Returns Highest Index but raises when substring is not found
rjust(width[, fillchar]) Returns the string right justified
rpartition(sep) Returns a tuple
rsplit(sep=None, maxsplit=-1) Splits String From Right
rstrip([chars]) Removes Trailing Characters
split(sep=None, maxsplit=-1) Splits String from Left
splitlines([keepends]) Splits String at Lines
startswith(prefix[, start[, end]]) Checks if String Starts with the Specified String
strip([chars]) Removes Both Leading and Trailing Characters
swapcase() swap uppercase characters to lowercase and vice versa
title() Returns a Title Cased String
translate(table) returns mapped charactered string
upper() returns uppercased string
zfill(width) Returns a Copy of The String Padded With Zeros