String
- String are the sequence of zero or more characters represented with in quotation marks. An empty string contains no character and has a length 0
- The indices of a string’s characters are numbered from 0 from the left end and numbered from -1 from the right end.
| Character | W | E | L | C | O | M | E |
|---|---|---|---|---|---|---|---|
| Index from left end | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
| Index from right end | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
Subscript Operator
- [ ] is a subscript operator that is used to access the characters at a time.
- Index within [ ] indicated the position of the particular character in the string and it must be an integer expression.
Syntax:
<stringname>[<index>]
Program
s=“HELLO”
s[0]
print(s[3])
Output
H
L
Len funtion
len is a built-in function that return the number of character in a string
Program
s=“HELLO PYTHON!”
print(“Length is”,len(s))
Output
Length is 13
In operators
- “in” is a Boolean operator that takes two strings as its operands and returns True if tha first string appears as a substring in the second string.
Program
print(“PY” in “HELLO PYTHON!”)
“NO” in “HELLO PYTHON!”
Output
True
False
String slices
- A part of a sting is called slice. The operator [m:n] returns the part of the string from 𝑚^𝑡ℎ index to 𝑛^𝑡ℎ index, including the character at 𝑚^𝑡ℎ index but excluding the character at 𝑛^𝑡ℎ index.
- If the first index is omitted, the slice starts at the beginning of the string. If the second index is omitted, the slice starts at the beginning of the string.
- If the first index is grater than or equal to the second, the slice is an empty string. If both indices are omitted, the slice is given string itself.
Program
str=‘Hello World’
| Code | Output |
|---|---|
| print(str) | Hello World |
| print(str[0]) | H |
| print(str[-1]) | d |
| print(str[1:5]) | ello |
| print(str[2:]) | llo World |
| print(str[3:3]) | |
| print(str[:]) | Hello World |
| print(str[-3:]) | rld |
Ability
A string is an immutable data structure. This means that its characters can be accessed but the string cannot be modified
Program:
Output
Output
String Methods
Built-in String Methods
Python includes the following built-in methods to manipulate strings
| S.NO. | Method | Description |
|---|---|---|
| 1 | capitalize() | Capitalizes the first letter of the string. |
| 2 | center(width, fillchar) | Returns a space-padded string with the original string centered to a total of width columns. |
| 3 | count(str, beg=0, end=len(string)) | Counts how many times str occurs in string or in a substring of string if starting index beg and ending index end are given. |
| 4 | decode(encoding='UTF-8', errors='strict') | Decodes the string using the codec registered for encoding. Encoding defaults to the default string encoding. |
| 5 | encode(encoding='UTF-8', errors='strict') | Returns encoded string version of string; on error, default is to raise a ValueError unless error is given with 'ignore' or 'replace'. |
| 6 | endswith(suffix, beg-=0 , end=len(string)) | Determines if string or a substring (if starting index beg and ending index end are given) ends with suffix; returns true if so and false otherwise. |
| 7 | expandtabs(tabsize=8) | Expands tabs in string to multiple spaces; defaults to 8 spaces per tab if tabsize not provided. |
| 8 | find(str, beg=0, end=len(string)) | Determine if str occurs in string or in a substring of string if starting index beg and ending index end are given returns index if found and -1 otherwise. |
| 9 | index(str, beg=0, end=len(len(string))) | Same as find(), but raises an exception if str not found. |
| 10 | isalnum() | Returns true if string has at least 1 character and all characters are alphanumeric and false otherwise. |
| 11 | isalpha() | Returns true if string has al least 1 character and all characters are alphabetic an false otherwise. |
| 12 | isdecimal() | Returns true if a unicode string contains only decimal characters and false otherwise |
| 13 | isdigit() | Returns true if string contains onlu digits and false otherwise. |
| 14 | islower() | Returns true if string has at lease 1 cased character and all cased characters are in lowercase and false otherwise. |
| 15 | isnumeric() | Returns ture if a unicode string contains only numeric characters and false otherwise. |
| 16 | isspace() | Returns ture if string contains only whitespace characters and false otherwise. |
| 17 | istitle() | Returns true if string is properly "titlecased" and false otherwise. |
| 18 | isupper() | Returns ture if string has at least one cased character and all cased characters are in uppercase and false otherwise. |
| 19 | join(seq) | Merges (concatenates) the string representations of elements in sequence seq into a string, with separator string. |
| 20 | len(string) | Returns the length of the string. |
| 21 | ljust(width, fillchar) | Returns a space-padded string with the original string left-justified to a total of width columns. |
| 22 | lower() | Converts all uppercase letters in string to lowercase. |
| 23 | lstrip() | Removing all leading whitespace in string. |
| 24 | maketrans() | Returns a translation table to be used in translate funtion. |
| 25 | max(str) | Returns the max alphabetical character from the string str. |
| 26 | min(str) | Returns the min alphabetical character from the string str. |
| 27 | replace(old, new, max) | Replace all occurrence of old in string with new or at most max occurrences if max given |
| 28 | rfind(str, beg=0, end=len(string)) | Same as find(), but search backward in string. |
| 29 | rindex(str, beg=0, end=len(string)) | Same as index(), but search backward in string. |
| 30 | rjust(width, fillchar) | Returns a space-padded string with the original string left-justified to a total of width columns. |
| 31 | rstrip() | Remove all trailing whitespace of string. |
| 32 | split(str="", num=string.count(str)) | Splits string according to delimiter str(space if not provided) and returns list of substrings; split into at most num substrings if given. |
| 33 | splitlines(num=string.count(str)) | Strip string at all (or num) NEWLINEs and returns a list of each line with NEWLINEs removed. |
| 34 | startswith(str, beg=0, end=len(string)) | Determines if string or a substring of string (if starting index beg and ending index end are given) starts with substring str; returns true if so and false otherwidsotherwise. |
| 35 | strip() | Performs both lstrip() and rstrip() on string. |
| 36 | swapcase() | Inverts case for all letters in string. |
| 37 | title() | Returns "titlecased" version of string, that is, all words begin with uppercase and the rest are lowercase/ |
| 38 | translate(table, deletechars="") | Translates string according to translation table str(256 chars), removing those in del string. |
| 39 | upper() | Converts lowercase letters in string to uppercase. |
| 40 | zfill(width) | Returns original string leftpadded with zeros to a total of width characters; intended for numbers, zfill() retains any sign given (less one zero). |

No comments:
Post a Comment