Strings in python


Strings





Contents:-





  1. Introduction
  2. Compound data type
  3. String length
    1. Using the built-in function len.
    1. Using for loop and in operator.
    1. Using while loop and Slicing.
    1. Using string methods join and count.




  1. Introduction




Strings are arrays of bytes representing Unicode characters.





It is collection of one or more characters put in a single quote, double-quote or triple quote.





A character is a string which length is one.





It is represented by str class.





The expression in brackets is called an index.





The index indicates sequence of character.





s = "This is a string"





print(s)





s = '''A multiline





string'''





print(s)





Output





This is a string





A multiline





string





Like a list and tuple, the slicing operator [ ] can be used with strings.





Strings are immutable.





s = 'Hello world!'





# s[4] = 'o'





print("s[4] = ", s[4])





# s[6:11] = 'world'





print("s[6:11] = ", s[6:11])





# Generates error





# Strings are immutable in Python





s[5] ='d'





Output





s[4] =  o





s[6:11] =  world





Traceback (most recent call last):





  File "<string>", line 11, in <module>





TypeError: 'str' object does not support item assignment





  • String as a compound data type




String are compound data types - meaning they are made up of smaller pieces of alphabets, numbers, Special symbols and white space etc..





Strings, are made up of characters or words.





Empty string, containing no characters.





  • String Length




We can find length of string by 4 ways:-





  1. Using the built-in function len.
  2. Using for loop and in operator.
  3. Using while loop and Slicing.
  4. Using string methods join and count.




 





  1. Using the built-in function len.




The built-in function len returns the number of items in a container.





returns the number of characters in a string.





Example:-





>>> fruit = 'banana'





>>> len(fruit)





6





To get the last letter of a string.





have to subtract 1 from length.





>>> length = len(fruit)





>>> last = fruit[length]





Output:- IndexError: string index out of range





The reason for the IndexError is that there is no letter in 'banana' with the index 6 and we started counting from zero in string index, the six letters are numbered 0 to 5.





Negative indices count backward from the end of the string.





>>> last = fruit[length-1]





>>> print last





a





Ex. fruit[-1] yields the last letter, fruit[-2] yields the second to last, and so on.





Example:-





str = "abcde" print(len(str))




Output:





5





  1. Using for loop and in operator.




A string can be iterated over, directly in a for loop.





iterations counting the number is length of the string.





# Returns length of string def findLen(str):     counter = 0         for i in str:         counter += 1     return counter  str = "abcde" print(findLen(str))




Output:





5





  • Using while loop and Slicing.




Slice a string making it smaller by 1 at each iteration till an empty string.





This is when while loop stops.





def findLen(str):     counter = 0     while str[counter:]:         counter += 1     return counter    str = "abcde" print(findLen(str))




Output:





5





  • Using string methods join and count.




The join method of strings takes in an iterable and returns a string which is the concatenation of the strings in the iterable.





The separator between the elements is the original string on which the method is called slice. Using join and counting the joined string in the original string will show the length of the string.





def findLen(str):     if not str:         return 0     else:         some_random_str = '#'         print ((some_random_str).join(str))         print ((some_random_str).join(str).count(some_random_str))                       return ((some_random_str).join(str)).count(some_random_str) +1   str = "abcde" print("total length of string ", findLen(str))




Output:





a#b#c#d#e





4





total length of string  5 ================================================================


Post a Comment

0 Comments