|
11.2.2.3 apply functions and string processing methods Python. Strings. Functions and methods
Function/Method
Example
Description
s1 + s2 s1 += s2 s1 = "Hello " Concatenation of strings means all characters from s1 come first,
and then all characters from s2 s1 in s2
s1 = "own" Checking that substring s1 is contained in s2
s1 not in s2
s1 = "own" Checking that substring s1 is not contained in string s2
The same as not (s1 in s2) s[i]
'school'[2] == 'h' i-th element of the string,
negative i - for counting from the end s[start:stop:step]
s = 'school' print(s[:2]) # 'sc' Slice a string
s.count(s2, a1, an)
s = "abracadabra" count() method returns the number of occurrences of the substring in the given string.
s1.find(s2)
s1.rfind(s2) s = 'abracadabra' The index of the beginning of the first or last occurrence of the substring s2 in s1 (will return -1 if s2 not in s1)
s.startswith(s2)
s.endswith(s2) print('school'.startswith('sc')) # True
print('school'.endswith('ml')) # False Checking if s starts with s2 s.isdigit() s.isalpha() s.isalnum() print('123'.isdigit()) # True print('abc'.isalpha()) # True print('E315'.isalnum()) #True Checking that all characters in string s are numbers, letters (including Cyrillic), numbers or letters s.islower()
print('hi!'.islower()) # True print('PY123'.isupper()) # True Checking that the string s does not contain large letters, small letters. s.lower() s.upper() print('Welcome!'.lower()) # welcome! print('Welcome!'.upper()) # WELCOME! The string s, in which all letters are converted to lower or upper case, s.capitalize() s.title() print('welcome back'.capitalize()) print('welcome back'.title()) String s, in which the first letter is uppercase String s, in which all words will be capitalized. s.lstrip() s.rstrip() s.strip() print(' Welcome! '.lstrip()) # 'Welcome! ' print(' Welcome! '.rstrip()) # ' Welcome!' print(' Welcome! '.strip()) # 'Welcome!' String s with whitespace characters (spaces, tabs) removed from the beginning, or both sides s.ljust(k, c) s.rjust(k, c) print('Comment'.ljust(10, '.')) print('Comment'.rjust(10, '.')) Adds the required amount to the left or right s.join(a) print(' '.join(['Computer', 'science'])) Concatenates (joins) strings from list a through s
s.split(s2) s = "Computer science" print(s.split()) # ["Computer", "science"] List of all words of string s (substrings separated by strings s2)
s.replace(s2, s3) s = "School" print(s.replace('o', 'e')) # Scheel String s, in which all occurrences of s2 are replaced by s3
There is an optional third parameter, with which you can specify how many times to replace list(s) s = "School" print(list(s)) # ['S', 'c', 'h', 'o', 'o', 'l'] List of characters from string s
bool(s) s1 = "something" print(bool(s1)) # True Checking that the string is not empty (returns True if not empty and False otherwise)
int(s) float(s) s = "50" print(int(s) + 4) # 54 If string s contains an integer or
(fractional) number, get this number, otherwise, it's an error str(x) n = 37 print(str(n) + "2") # 372 Convert any object x as a string
Questions: Exercises: Ex. 1 Ex. 2 Identify the result of outputs (Designed by Mr. Halil Mali - CS teacher of NIS Uralsk)
Ex. 3 Identify the result of outputs (Designed by Mr. Halil Mali - CS teacher of NIS Uralsk) Ex. 4 Identify result of outputs (Designed by Mr. Halil Mali - CS teacher of NIS Uralsk) Ex. 5 Identify the result of outputs (Designed by Mr. Halil Mali - CS teacher of NIS Uralsk) Ex. 6 Identify the result of outputs (Designed by Mr. Halil Mali - CS teacher of NIS Uralsk) Ex. 7. Quest "String methods" Tasks: | |
|
| |
| Просмотров: 45 | | |
| Всего комментариев: 0 | |