KEMBAR78
String Notes | PDF | Ascii | String (Computer Science)
0% found this document useful (0 votes)
15 views5 pages

String Notes

The document provides an overview of Python strings, including escape characters, string operators, formatting, and built-in methods. It details various escape sequences, string manipulation techniques, and formatting options available in Python. Additionally, it lists standard ASCII characters and their hexadecimal representations.

Uploaded by

sonenef143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

String Notes

The document provides an overview of Python strings, including escape characters, string operators, formatting, and built-in methods. It details various escape sequences, string manipulation techniques, and formatting options available in Python. Additionally, it lists standard ASCII characters and their hexadecimal representations.

Uploaded by

sonenef143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python - Strings

Escape Characters
Backslash Hexadecimal
Description
notation character
\a 0x07 Bell or alert
\b 0x08 Backspace
\cx Control-x
\C-x Control-x
\e 0x1b Escape
\f 0x0c Formfeed
\M-\C-x Meta-Control-x
\n 0x0a Newline
\nnn Octal notation, where n is in the range 0.7
\r 0x0d Carriage return
\s 0x20 Space
\t 0x09 Tab
\v 0x0b Vertical tab
\x Character x
Hexadecimal notation, where n is in the range 0.9,
\xnn
a.f, or A.F

String Special Operators


Operator Description Example
Concatenation - Adds values on either
+ a + b will give HelloPython
side of the operator
Repetition - Creates new strings,
* concatenating multiple copies of the a*2 will give –HelloHello
same string
Slice - Gives the character from the
[] a[1] will give e
given index
Range Slice - Gives the characters from
[:] a[1:4] will give ell
the given range
Membership - Returns true if a character
in H in a will give 1
exists in the given string
Membership - Returns true if a character
not in M not in a will give 1
does not exist in the given string
Raw String - Suppresses actual meaning
of Escape characters. The syntax for raw
strings is exactly the same as for normal
r/R print r'\n' prints \n and print R'\n'prints \n
strings with the exception of the raw
string operator, the letter "r," which
precedes the quotation marks. The "r"
can be lowercase (r) or uppercase (R)
and must be placed immediately
preceding the first quote mark.
% Format - Performs String formatting

String Formatting Operator


Format Symbol Conversion
%c character
%s string conversion via str() prior to formatting
%i signed decimal integer
%d signed decimal integer
%u unsigned decimal integer
%o octal integer
%x hexadecimal integer (lowercase letters)
%X hexadecimal integer (UPPERcase letters)
%e exponential notation (with lowercase 'e')
%E exponential notation (with UPPERcase 'E')
%f floating point real number
%g the shorter of %f and %e
%G the shorter of %f and %E

Built-in String Methods


Sr.No. Methods with Description
capitalize() Capitalizes first letter of string
1
center(width, fillchar) Returns a space-padded string with the original string
2 centered to a total of width columns

count(str, beg= 0,end=len(string)) Counts how many times str occurs in string or in
3 a substring of string if starting index beg and ending index end are given.

decode(encoding='UTF-8',errors='strict') Decodes the string using the codec


4 registered for encoding. encoding defaults to the default string encoding.

encode(encoding='UTF-8',errors='strict') Returns encoded string version of string;


on error, default is to raise a ValueError unless errors is given with 'ignore' or
5
'replace'.

endswith(suffix, beg=0, end=len(string)) Determines if string or a substring of


string (if starting index beg and ending index end are given) ends with suffix;
6
returns true if so and false otherwise.
expandtabs(tabsize=8) Expands tabs in string to multiple spaces; defaults to 8
7 spaces per tab if tabsize not provided.

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
8
and -1 otherwise.

index(str, beg=0, end=len(string)) Same as find(), but raises an exception if str not
9 found.

isalnum() Returns true if string has at least 1 character and all characters are
10 alphanumeric and false otherwise.

isalpha() Returns true if string has at least 1 character and all characters are
11 alphabetic and false otherwise.

isdigit() Returns true if string contains only digits and false otherwise.
12
islower() Returns true if string has at least 1 cased character and all cased characters
13 are in lowercase and false otherwise.

isnumeric() Returns true if a unicode string contains only numeric characters and
14 false otherwise.

isspace() Returns true if string contains only whitespace characters and false
15 otherwise.

istitle() Returns true if string is properly "titlecased" and false otherwise.


16
isupper() Returns true if string has at least one cased character and all cased
17 characters are in uppercase and false otherwise.

join(seq) Merges (concatenates) the string representations of elements in sequence


18 seq into a string, with separator string.

len(string) Returns the length of the string


19
ljust(width[, fillchar]) Returns a space-padded string with the original string left-
20 justified to a total of width columns.

21 lower() Converts all uppercase letters in string to lowercase.

lstrip() Removes all leading whitespace in string.


22
maketrans() Returns a translation table to be used in translate function.
23
max(str) Returns the max alphabetical character from the string str.
24
min(str) Returns the min alphabetical character from the string str.
25
replace(old, new [, max]) Replaces all occurrences of old in string with new or at
26 most max occurrences if max given.

rfind(str, beg=0,end=len(string)) Same as find(), but search backwards in string.


27
rindex( str, beg=0, end=len(string)) Same as index(), but search backwards in
28 string.

rjust(width,[, fillchar]) Returns a space-padded string with the original string right-
29 justified to a total of width columns.

rstrip() Removes all trailing whitespace of string.


30
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
31
given.

splitlines( num=string.count('\n')) Splits string at all (or num) NEWLINEs and


32 returns a list of each line with NEWLINEs removed.

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;
33
returns true if so and false otherwise.

34 strip([chars]) Performs both lstrip() and rstrip() on string.

swapcase() Inverts case for all letters in string.


35
title() Returns "titlecased" version of string, that is, all words begin with uppercase
36 and the rest are lowercase.

translate(table, deletechars="") Translates string according to translation table


37 str(256 chars), removing those in the del string.

upper() Converts lowercase letters in string to uppercase.


38
zfill (width) Returns original string leftpadded with zeros to a total of width
39 characters; intended for numbers, zfill() retains any sign given (less one zero).

isdecimal() Returns true if a unicode string contains only decimal characters and
40
false otherwise.
Standard characters

ASCII Hex Symbol ASCII Hex Symbol ASCII Hex Symbol ASCII Hex Symbol

0 0 NUL 16 10 DLE 32 20 (space) 48 30 0


1 1 SOH 17 11 DC1 33 21 ! 49 31 1
2 2 STX 18 12 DC2 34 22 " 50 32 2
3 3 ETX 19 13 DC3 35 23 # 51 33 3
4 4 EOT 20 14 DC4 36 24 $ 52 34 4
5 5 ENQ 21 15 NAK 37 25 % 53 35 5
6 6 ACK 22 16 SYN 38 26 & 54 36 6
7 7 BEL 23 17 ETB 39 27 ' 55 37 7
8 8 BS 24 18 CAN 40 28 ( 56 38 8
9 9 TAB 25 19 EM 41 29 ) 57 39 9
10 A LF 26 1A SUB 42 2A * 58 3A :
11 B VT 27 1B ESC 43 2B + 59 3B ;
12 C FF 28 1C FS 44 2C , 60 3C <
13 D CR 29 1D GS 45 2D - 61 3D =
14 E SO 30 1E RS 46 2E . 62 3E >
15 F SI 31 1F US 47 2F / 63 3F ?

ASCII Hex Symbol ASCII Hex Symbol ASCII Hex Symbol ASCII Hex Symbol

64 40 @ 80 50 P 96 60 ` 112 70 p
65 41 A 81 51 Q 97 61 a 113 71 q
66 42 B 82 52 R 98 62 b 114 72 r
67 43 C 83 53 S 99 63 c 115 73 s
68 44 D 84 54 T 100 64 d 116 74 t
69 45 E 85 55 U 101 65 e 117 75 u
70 46 F 86 56 V 102 66 f 118 76 v
71 47 G 87 57 W 103 67 g 119 77 w
72 48 H 88 58 X 104 68 h 120 78 x
73 49 I 89 59 Y 105 69 i 121 79 y
74 4A J 90 5A Z 106 6A j 122 7A z
75 4B K 91 5B [ 107 6B k 123 7B {
76 4C L 92 5C \ 108 6C l 124 7C |
77 4D M 93 5D ] 109 6D m 125 7D }
78 4E N 94 5E ^ 110 6E n 126 7E ~
79 4F O 95 5F _ 111 6F o 127 7F •

You might also like