Strings in Python
We generally use strings in python to do operations characters. Strings are immutable objects that mean we cannot delete or update a string once it is declared or assigned to a variables.
String is type of immutable data type of python. String is defined either in double code "Cleo" or in single 'Cleo'. Creating strings is as simple as assigning a value to a variable.
Accessing string elements (String Slicing)
#Defining string
str = 'Goeduhub Technologies 12345'
print('str = ', str)
#first character
print('str[0] = ', str[0])
#last character
print('str[-1] = ', str[-1])
#slicing 1nd to 5th character
print('str[1:5] = ', str[1:5])
#slicing 5th to 2nd last character
print('str[5:-2] = ', str[5:-2])
#space cosider as string element
print('str[8]=',str[8])
Output
str = Goeduhub Technologies 12345 str[0] = G str[-1] = 5 str[1:5] = oedu str[5:-2] = hub Technologies 123 str[8]=
Note: As you can see from the above output string is a heterogeneous type of data type.
Indexing of a String
Indexing of string is similar to list
Note: From left to right is started from 0 to n, and from right to left it started from -1 to -n.
n- here n is last element of the string
Basic Operation on String str1 = 'Python'
str2 ='String'
concatenating strings
print('str1 + str2 = ', str1 + str2)
printing string multiple time
print('str1 2 =', str1 2)
Output
str1 + str2 = PythonString
str1 * 2 = PythonPython
DELETING AND UPDATING A STRING Str1 = "Python Programming"
print(Str1)
Updating a element of string
Str1[2] = 'a'
print(Str1)
Output
Note: As we know string is immutable means we can't update/ delete a element of string. If we try it, it will give us a error as shown in above output.So, conclusion is if you want to delete an element of a string you have to delete entire string.
So, basically creating a new string is the solution of this problem, see the example below
Str1 = "Python Programming"
Deleting a element of string
del Str1
Creating new string
Str1= "Creating new string"
print(Str1)
Output
Creating new string
String Formatting
Default order
Str1 = "{} {} ".format('python', 'programming')
print(Str1)
Positional Formatting
Str2= "{1} {0} ".format('python','programming')
print(Str2)
Keyword Formatting
Str1 = "{g} {t} {j}".format(g = 'Goeduhub', t= 'Technologies', j = 'Jaipur')
print(Str1)
Output
python programming programming python Goeduhub Technologies Jaipur
String Function and Methods Python has number of functions and methods but here you can see some important function and methods, see the table below
Function/ Method Description string.lowercase A string must contain lowercase letters. string.uppercase A string must contain uppercase letters. String.index Returns the position of the first occurrence of substring in a string replace() returns a copy of the string where all occurrences of a substring is replaced with another substring. String.Isalnum Returns true if all the characters in a given string are alphanumeric. String.len Returns the length of the string. String.Max Returns the highest alphabetical character in a string. string.capitalize Return a word with its first character capitalized. string.split Return a list of the words of the string,If the optional second argument sep is absent or None string.strip() It return a copy of the string with both leading and trailing characters removed We can also access string elements using loop
count = 0
for letter in 'Goeduhubuu':
if(letter == 'u'):
count += 1
print(count,' times u letter found in given string')
Output
4 times u letter found in given string
How to use function/ Method in string
str1= "Goeduhub Python programming"
print(len(str1),"Length of given string")