Lecture 4

String

  • String indexing
  • String slicing
  • String Formatting

  • function with string
  • String Boolean Method


Muliply Input with map function



String

In Python, A string is a sequence of characters enclosed within a single quote or double quote. These characters could be anything like letters, numbers, or special symbols enclosed within double quotation marks. For example, 'supawit'or "supawit" is a string.

Using quotes within string


  • String indexing

Indexing is used to access the characters of a string. Indexing starts from 0 from left to right and -1 from right to left.


String are immutable in python once they are created they cannot be changed some operations such as concatenation.but in reallity they do not.





    • String slicing

    String slicing is used to access the substring of a string. It is done by specifying the start and end index of the substring separated by a colon. The start index is included and the end index is excluded.

Example







  • FUNCTION LEN()

len() function is used to find the length of a string.






  • FUNCTION LOWER(),UPPER(),FORMAT()

lower() function is used to convert the string into lower case.
upper() function is used to convert the string into upper case.
format() function is used to format the string.


STRING CONCATENATION

String concatenation is used to join two or more strings.




  • FORMATTING STRING

Formatting string is used to format the string.


"JOIN"

join() function is used to join the string.


"SPLIT"

split() function is used to split the string.


"STRIP"

strip() function is used to remove the white space from the string.

  • lstrip() function is used to remove the white space from the left side of the string.

  • rstrip() function is used to remove the white space from the right side of the string.

  • strip() function is used to remove the white space from both side of the string.See the example below.

"TITLE"

title() function is used to convert the first letter of the word into capital letter.




"CAPITALIZE"





  • Capitalize() function is used to convert the first letter of the string into capital letter.


SEARCH AND REPLACE

  • endswith() function is used to check whether the string ends with the given string or not.return true if the string ends with the given string otherwise false.

  • startswith() function is used to check whether the string starts with the given string or not.return true if the string starts with the given string otherwise false.


  • find() function is used to find the index of the given string in the given string.return -1 if the given string is not found in the given string.


  • replace() function is used to replace the given string with the given string. replace(old,new)


Example


  • STRING BOOLEAN METHOD

Method True if
isalnum() if all the characters are alphanumeric
isalpha() if all the characters are alphabetic
islower() if all the characters are lowercase
isupper() if all the characters are uppercase
isspace() if all the characters are whitespace
istitle() if the string is titlecased
isdecimal() if all the characters are decimal
isnumeric() if all the characters are numeric
isidentifier() if the string is a valid identifier



Last Example


Thanks !

If you have any questions, please feel free to contact me.