Python Strings

Python Strings:

In Python, String literals are characterized either by single quotation marks, or double quotation marks surrounding them. The string literals can be single line or multiple line strings. Strings in Python is a list, where each character represents an element of the list.

Python String Methods:

METHODS USES
capitalize() To convert the first character of a string to uppercase.
casefold() To convert a complete string into lowercase.
center() To get a centered string.
count() To get the number of times a specified value occurs in a string.
encode() To get an encoded version of the string.
endswith() To get a true value if the string ends with the specified value.
expandtabs() To set the tab size of the string.
find() To search the string for a specified value and to get its position.
format() To format the specified values in a string.
format_map() To format the specified values in a string.
index() To search the string for a specified value and to get its position.
isalnum() To get a true value if all characters in the string are alphanumeric.
isalpha() To get a true value if all characters in the string are alphabets.
isdecimal() To get a true value if all characters in the string are decimals.
isdigit() To get a true value if all characters in the string are digits.
isidentifier() To get a true value if the string is an identifier.
islower() To get a true value if all characters in the string are lower case.
isnumeric() To get a true value if all characters in the string are numeric.
isprintable() To get a true value if all characters in the string are printable.
isspace() To get a true value if all characters in the string are whitespaces.
istitle()  To get a true value if the string follows the rules of a title.
isupper() To get a true value if all characters in the string are uppercase.
join() To join the elements of an iterable to the end of the string.
len() To return the length of a string.
ljust() To return a left justified version of the string.
lower() To convert a string into lowercase.
lstrip() To return a left trim version of the string.
maketrans() To return a translation table to be used in translations.
partition() To return a tuple where the string is parsed into three parts.
replace() To return a string where a specified value is replaced with a specified value.
rfind() To search the string for a specified value and to get its last position.
rindex() To search the string for a specified value and to get its last position.
rpartition() To return a tuple where the string is parsed into three parts.
rsplit() To split the string at the specified separator, and to get a list.
rstrip() To return a right trim version of the string.
strip() To remove any whitespace from the beginning or the end of the string.
split() To split the string at the specified separator, and to get a list.
splitlines() To split  the string at line breaks and to get a list.
startswith() To get a true value if the string starts with the specified value.
swapcase() To swap lowercase to uppercase and vice versa.
title() To convert the first character of each word to uppercase.
translate() To get a translated string.
upper() To convert a string into uppercase.
zfill() To fill the string with a specified number of 0 values at the beginning.

Python String Operators:

OPERATORS SYMBOLS USES
String Concatenation Operator + To concatenate two Strings and creating a new String.
Python String Replication Operator * To repeat a string number of times.
In Operator in To return a true value if a character or the entire substring is present in the specified string, otherwise returns false.
Not In Operator not in To return a true value if a character or entire substring does not exist in the specified string, otherwise false.
Python Relational Operators

 

<,><=,>=,==,!=,<> To compare two strings based on their ASCII value.

 

Example:

a = "I Am Python."
print a
print "\n"
 
print "Get the character at position 0."
print(a[0])
print "\n"
 
print "Get the characters from position 1 to position 6."
print(a[1:6])
print "\n"
 
print "Remove all whitespace."
print(a.strip())
print "\n"
 
print "Get the total length."
print(len(a))
print "\n"
 
print "Get the string in lower case."
print(a.lower())
print "\n"
 
print "Get the string in upper case."
print(a.upper())
print "\n"
 
print "Replace a character."
print(a.replace("I", "M"))
print "\n"
 
print "Split the string."
print(a.split(" "))
print "\n"

Output:

Please follow and like us:
Content Protection by DMCA.com