String in King: The True Power of Text in Python
Let’s Bind with Strings and Discover Their Eternity.Let’s go deeper and get to know in and out about strings so that we can get to know about great power of string.
In python Strings are combination of Characters that creates a Text.and Strings are immutable meaning after creation of string the value cannot be changed.
So it defines as
name="trina"
There are many string operations with which we can get in and out about strings.
type(name)
We can get string length by
len(string)
Memory Management in String

So this is how character by character string memory allocated.
We can get access of each character by following way.
string = "trina"
second_char = string[1]
last_char = string[-1]
print(second_char)
print(last_char)
Output:
r,a
Now I am going to discuss about operations related to String.
Slicing Operation
string[start:end:step_size]
There is three parameter while doing this operation.First one is start index of string,second parameter in end index of string and last one is step size.How much it will skip while showing data.
Examples
string = "Hello, World!"
print(string[1:4])#ell
print(string[1:8:2])#el,W
print(string[:5]) #Hello
print(string[7:]) #World!
print(string[-4:]) #rld!
print(string[:-5])#Hello, W
#Reversal of the entire string
print(string[::-1]) #!dlroW ,olleH
print(string[::]) #Hello, World!
print(string[::-2]) #!lo olH
These are different ways for slicing a string.
Inbuilt Functions of String
There are many inbuilt functions in string.That are given below.You know,These in built function are self explained so by its name you can get idea what task these methods are assigned for.
string = "Hello, World!"
uppercase_string = string.upper()
print(uppercase_string) #HELLO, WORLD!
lowercase_string = string.lower()
print(lowercase_string) #hello, world!
string = "Today's session is quite fun"
titlecase_string = string.title()
print(titlecase_string) #Today'S Session Is Quite Fun
capitalized_string = string.capitalize()
print(capitalized_string)#Today's session is quite fun
In titlecase all the first letter of words get to uppercase where for capitalize only first letter of sentence converted to uppercase.
str1 = "Hello"
str2 = "World"
string = "Today's session is quite fun"
# Concatenation Operation
result = str1 + " " + str2
print(result)#Hello World
result = str1 * 3
print(result)#HelloHelloHello
print(string[1:8:2])#oa'
Comparison in String
string1 = "apple"
string2 = "banana"
print(string1 == string2) ## Equal to
print(string1 != string2) ## Not equal to
print(string1 < string2) ## Less than
print(string1 > string2) ## Greater than
print(string1 <= string2) ## Less than or equal to
print(string1 >= string2) ## Greater than or equal to
Output:
False True True False True False
In string comparison happens with respect to ASCII value.If two ascii value of variables are same it will look for next value.So it compare character by character with respect to ASCII value.
We can get ASCII value of a character by following code:
ord('a')#97
Some more important operations in Python
replace()
original_string = "Hello, world!"
## Replace "world" with "Python"
new_string = original_string.replace("world", "Python")
print(new_string)
Output: Hello, Python!
split()
sentence = "This is a very interesting session"
words = sentence.split()
print(words)
names = "Rohit, Monika, Shyam, Rashmi, Priya, Vidushi, Aranya"
names_list = names.split(',')
print(names_list)
Output:['This', 'is', 'a', 'very', 'interesting', 'session']
['Rohit', ' Monika', ' Shyam', ' Rashmi', ' Priya', ' Vidushi', ' Aranya']
Formatting
age = 30
text = "My name is Priya, I am " + age
print(text)
In Python we cannot append string with number.so this will give an error TypeError: can only concatenate str (not "int") to str.
So concatenating string with integer we can do following operations.
age = 30
text = f"My name is Priya, I am {age} years old"
print(text)
strip()
It removes any whitespace from the beginning or the end.
text = " Hello, Python Developers! "
stripped_text = text.strip()
print(stripped_text)#Hello, Python Developers!
index()
string = "Hello, World!"
index = string.index("World")
print(index)
If value is not present in the string index will provide an errror :"ValueError: substring not found”
string = "Hello, World!"
index = string.index("Python")
print(index)
find()
If value is not present it will give -1 and if present will return index of that value.And always return index for first occurance of value.
string = "Hello, World!"
index = string.find("World")
print(index)#7
index = string.find("Python")
print(index)#-1
sentence = "This is a sample sentence"
count = sentence.count("is")
print(count)
Below methods will check if alphabet only present or not .If yest will return True else False.
text1 = "Hello"
text2 = "Hello123"
print(text1. isalpha())#True
Count
sentence = "This is a sample sentence"
count = sentence.count("a")
print(count)#2text1 = "HELLO"
Some operations to check capitalize,lower or digit is present or not.These always return boolean value.
text1 = "HELLO"
text1.isupper() #true
text1.islower()#False
text1.isdigit()#False
Swapcase
text = "Hello World! 1234"
swapped_text = text.swapcase()
print(swapped_text)
These are some in bulit operations that we can do with strings.There are many more operations other than these.But these are most used operation using strings.So that's all about string and it's operation.