Fun with String

In python, there are some very useful basic string functions that come handy while manipulating strings as per your need. For all functions we will take below string as example

String = "Some big string"

String1 = "Pyghost"

In above string position of P is 0 and position of t is 6. In case if you want to print specific charater at specific position you can print using

print(String1[0])
P
print(String1[6])
t

in case if you wants to print from character 2 to 4 then

print(String1[2:4])
gh

in case if you wants to print from 2nd character and you don't know length then

print(String1[2:])
ghost

in case if you wants to pring last 2 character and you don't know length then

print(String1[-2:]
st

 

len()

This function is used to get length of string to use it.

len(String1)
7

Result will be 7 as this string have 7 characters. but positions of character will be 0-6

 

find()

Find function is used to find specific string within string.

String1.find("h")
3

Result will be 3 as h is on 3rd position, remember counting start from 0 not from 1, you can serch for string also instead of character.

String.find("big")
5

here result will be 5 as "string" big starts from 5th position.

 

count()

Count string count how many time specific string occurs within a string.

String.count("i")
2

 

Below are more examples, which you can use

capitalize() Converts the first character to upper case
casefold() Converts string into lower case
center() Returns a centered string
count() Returns the number of times a specified value occurs in a string
encode() Returns an encoded version of the string
endswith() Returns true if the string ends with the specified value
expandtabs() Sets the tab size of the string
find() Searches the string for a specified value and returns the position of where it was found
format() Formats specified values in a string
format_map() Formats specified values in a string
index() Searches the string for a specified value and returns the position of where it was found
isalnum() Returns True if all characters in the string are alphanumeric
isalpha() Returns True if all characters in the string are in the alphabet
isdecimal() Returns True if all characters in the string are decimals
isdigit() Returns True if all characters in the string are digits
isidentifier() Returns True if the string is an identifier
islower() Returns True if all characters in the string are lower case
isnumeric() Returns True if all characters in the string are numeric
isprintable() Returns True if all characters in the string are printable
isspace() Returns True if all characters in the string are whitespaces
istitle() Returns True if the string follows the rules of a title
isupper() Returns True if all characters in the string are upper case
join() Joins the elements of an iterable to the end of the string
ljust() Returns a left justified version of the string
lower() Converts a string into lower case
lstrip() Returns a left trim version of the string
maketrans() Returns a translation table to be used in translations
partition() Returns a tuple where the string is parted into three parts
replace() Returns a string where a specified value is replaced with a specified value
rfind() Searches the string for a specified value and returns the last position of where it was found
rindex() Searches the string for a specified value and returns the last position of where it was found
rjust() Returns a right justified version of the string
rpartition() Returns a tuple where the string is parted into three parts
rsplit() Splits the string at the specified separator, and returns a list
rstrip() Returns a right trim version of the string
split() Splits the string at the specified separator, and returns a list
splitlines() Splits the string at line breaks and returns a list
startswith() Returns true if the string starts with the specified value
strip() Returns a trimmed version of the string
swapcase() Swaps cases, lower case becomes upper case and vice versa
title() Converts the first character of each word to upper case
translate() Returns a translated string
upper() Converts a string into upper case
zfill() Fills the string with a specified number of 0 values at the beginning

 

Try these functions and see the results.

 

About the author

akarperiwal

0 Comments

Please login to comment