PythonTutorials Lecture -3- By: Ali Sattar To: Everyone
Contents: 1. String Operations. 2. String Methods. 3. Examples.
There are multiple operations can be performed on strings in python, they are: 1.1 Equality: This means check two string if they both have exactly the same characters or characters or contents and the same length. This can be done using the operator (==), for example: A = “Apple”; B = “Brad”; (A == B) = False; (A != B) =True 1.2 Numerical: There are two numerical operations here: Addition and Multiplication. For For addition, it is used to concatenate a specific string with another string or substring. For multiplication, it is used as repetitive addition to concatenate the same string with itself according to which number is multiplied. 1. String Operations?
1. String Operations A = “I Love”; P=“Python”; A+” ” +P = “I Love Python”; P = “Python”; P * 4 = “PythonPythonPythonPython” 1.3 Containment: It describes a simple operator named “in” used to check if that substring is exist in the original string or not and returns true or false. A = “Ali Sattar”; a = “atta”; a in A =True; A in a = False 1.4 Indexing and Slicing: String in python like array in the other languages, it can be indexed (0 – (n-1)) in a specific string on length(n), so string positions cab be accessed: String_Name [Character_Pos]
1. String Operations A = “Ali Sattar”; A[0] = ‘A’; A[5]= ‘a’ Strings in python unlike arrays, it can be indexed in a backward; it means we can index from the right to left, for example: A [-2] = ‘a’; A[-8] = ‘i’ For slicing on string, this can be done when we want to access a substring exist in the original string and according to the following example: A[a:b] -> A[a:b-1], where a is the start (A character in which you will start) and b-1 the character in which you will stop ok. A = “Python Programming”; A[3:9] = “hon pr”; A[7: ] = “Programming”
There are several methods in string, so I will try the most of them: 2.1 capitalize: It is used to convert the first letter from the your string to upper case. 2.2 right, left and center: It is used to justify your string according to field size. 2.3 count: It is used to count no. of substring repeated in the original string. 2.4 isalnum: It is return true if your string is composed of letters and numeric only. No special symbol and other characters are not accepted here. 2.5 isalpha and isdigit: The first method return true if and only if your string is composed composed from letters.The second method return true if and only if your string is composed from digits. 2. String Methods
2. String Methods 2.6 isspace: This method return true if and only if your string is entirely empty “ “ or ‘’. 2.7 isupper, islower and ititle: The first method return true if your string contains a capital letters, the second method do the opposite.The las method return true if your string starts with an upper case letter followed by any series of lower case. 2.8 title: This method used to capitalize the first letter of each word containing in your string. 2.9 swapcase: This method convert any upper case letter in your string to lower case and converts the lower case to upper case too.
2.10 join: This method will join any other data type value with a string to get a string value. There is also map method with join it will convert the current value to string regardless of its data type. 2.11 find and index: Both of methods return the first occurrence of the given substring or letter. 2.12 len: This method used to return the length of string (How many characters in it). 2.13 replace: This method used to replace a specific character by another character exist in the original string. 2. String Methods
3. Examples A = “python”; B = “python syntax is easy”; C = “%compiler%”; L = [‘1’, ‘2’, ‘3’, ‘4’]; print(B.len()) = 21 print(A.capitalize()) = “Python” print(A.center(3)) = “ python “ print(B.count(‘s’)) = 3 print(C.isalnum()) = false print(B.isdigit()) = false print(A.isalpha()) = true print(A.isupper()) = false print(A.swapcase()) = “PYTHON” print(‘’.join(L)) = ‘1234’ print(‘+’.join(L)) = ‘1+2+3+4’ print(B.find(‘s’)) = 8 print(B.replace(‘s’, ‘l’) = “python lyntax il ealy”

String in python lecture (3)

  • 1.
  • 2.
    Contents: 1. String Operations. 2.String Methods. 3. Examples.
  • 3.
    There are multipleoperations can be performed on strings in python, they are: 1.1 Equality: This means check two string if they both have exactly the same characters or characters or contents and the same length. This can be done using the operator (==), for example: A = “Apple”; B = “Brad”; (A == B) = False; (A != B) =True 1.2 Numerical: There are two numerical operations here: Addition and Multiplication. For For addition, it is used to concatenate a specific string with another string or substring. For multiplication, it is used as repetitive addition to concatenate the same string with itself according to which number is multiplied. 1. String Operations?
  • 4.
    1. String Operations A= “I Love”; P=“Python”; A+” ” +P = “I Love Python”; P = “Python”; P * 4 = “PythonPythonPythonPython” 1.3 Containment: It describes a simple operator named “in” used to check if that substring is exist in the original string or not and returns true or false. A = “Ali Sattar”; a = “atta”; a in A =True; A in a = False 1.4 Indexing and Slicing: String in python like array in the other languages, it can be indexed (0 – (n-1)) in a specific string on length(n), so string positions cab be accessed: String_Name [Character_Pos]
  • 5.
    1. String Operations A= “Ali Sattar”; A[0] = ‘A’; A[5]= ‘a’ Strings in python unlike arrays, it can be indexed in a backward; it means we can index from the right to left, for example: A [-2] = ‘a’; A[-8] = ‘i’ For slicing on string, this can be done when we want to access a substring exist in the original string and according to the following example: A[a:b] -> A[a:b-1], where a is the start (A character in which you will start) and b-1 the character in which you will stop ok. A = “Python Programming”; A[3:9] = “hon pr”; A[7: ] = “Programming”
  • 6.
    There are severalmethods in string, so I will try the most of them: 2.1 capitalize: It is used to convert the first letter from the your string to upper case. 2.2 right, left and center: It is used to justify your string according to field size. 2.3 count: It is used to count no. of substring repeated in the original string. 2.4 isalnum: It is return true if your string is composed of letters and numeric only. No special symbol and other characters are not accepted here. 2.5 isalpha and isdigit: The first method return true if and only if your string is composed composed from letters.The second method return true if and only if your string is composed from digits. 2. String Methods
  • 7.
    2. String Methods 2.6isspace: This method return true if and only if your string is entirely empty “ “ or ‘’. 2.7 isupper, islower and ititle: The first method return true if your string contains a capital letters, the second method do the opposite.The las method return true if your string starts with an upper case letter followed by any series of lower case. 2.8 title: This method used to capitalize the first letter of each word containing in your string. 2.9 swapcase: This method convert any upper case letter in your string to lower case and converts the lower case to upper case too.
  • 8.
    2.10 join: Thismethod will join any other data type value with a string to get a string value. There is also map method with join it will convert the current value to string regardless of its data type. 2.11 find and index: Both of methods return the first occurrence of the given substring or letter. 2.12 len: This method used to return the length of string (How many characters in it). 2.13 replace: This method used to replace a specific character by another character exist in the original string. 2. String Methods
  • 9.
    3. Examples A =“python”; B = “python syntax is easy”; C = “%compiler%”; L = [‘1’, ‘2’, ‘3’, ‘4’]; print(B.len()) = 21 print(A.capitalize()) = “Python” print(A.center(3)) = “ python “ print(B.count(‘s’)) = 3 print(C.isalnum()) = false print(B.isdigit()) = false print(A.isalpha()) = true print(A.isupper()) = false print(A.swapcase()) = “PYTHON” print(‘’.join(L)) = ‘1234’ print(‘+’.join(L)) = ‘1+2+3+4’ print(B.find(‘s’)) = 8 print(B.replace(‘s’, ‘l’) = “python lyntax il ealy”