0x04 - string of Python sequence family

preface

catalog: 0x00 - Python learning notes
Some knowledge of lists and tuples mentioned in the previous article are very similar because they both belong to sequences.

A sequence is a continuous memory space that can hold multiple values. These values are arranged in a certain order and can be accessed through the number of the location of each value (called an index).
They also include strings, lists, tuples, sets, and dictionaries.
Their operations include indexing, slicing, adding, multiplying, and checking members.
Note, however, that sets and dictionaries do not support indexing, slicing, addition, and multiplication operations.

Sequence properties of string

  • Indexes
>>> str1 = 'ctf_is_fun!'
>>> str1[2]
'f'
  • section
>>> str1 = 'ctf_is_fun!'
>>> str1[2:5]
'f_i'
>>> str1[:5]
'ctf_i'
>>> str1[2:]
'f_is_fun!'
>>> str1[:]
'ctf_is_fun!'
  • plus
    Update the string by adding and slicing
>>> str1 = 'ctffun!'
>>> str1 = str1[:3] + '_is_' + str1[3:]
>>> str1
'ctf_is_fun!'

Other methods

In Python 3, all strings are Unicode strings

>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', 
'__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__',
 '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
  '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
   '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 
   'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 
   'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition',
    'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines',
    'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Its function is to return the corresponding form, and the original string is not modified

  • Convert to uppercase upper
>>> str1 = 'Hello world!'
>>> str1.upper()
'HELLO WORLD!'
>>> str1
'Hello world!'
  • To lowercase lower
>>> str1 = 'Hello World!'
>>> str1.lower()
'hello world!'
  • Initial capital capitalize
>>> str1 = 'hello'
>>> str1.capitalize()
'Hello'
>>> str1
'hello'
# change
>>> str1 = str1.capitalize()
>>> str1
'Hello world'
  • Capitalize each word title
>>> str1 = 'hello world!'
>>> str1.title()
'Hello World!'

Decoding common

Since it is network security, it is natural to use some commonly used functions that are easy to decode

  • Statistical times
    Count the number of occurrences of str in string: count(str, start= 0,end=len(string))
>>> str1 = 'asdqweadsadvsdasdcasdqqwecxz'
>>> str1.count('a')
5
>>> str1.count('a',3,20)
4
  • Encoding and decoding
    Encode and decode using encode and decode. There are other default parameters, which will not be introduced here
>>> str1 = 'flag{ctf_is_fun}'
>>> str1_base64 = str1.encode('base64')
>>> print('base64_encode:'+str1_base64)
base64_encode:ZmxhZ3tjdGZfaXNfZnVufQ==

>>> print('base64_decode:'+str1_base64.decode('base64'))
base64_decode:flag{ctf_is_fun}
  • Check whether there are letters and numbers
    str.isalnum()
>>> str1 = 'lalalalalal'
>>> str2 = 'lalala..lal'
>>> str1.isalnum()
True
>>> str2.isalnum()
False
  • The check consists only of numbers
    str.isdigit()
>>> str1 = '123456'
>>> str2 = '123546a'
>>> str1.isdigit()
True
>>> str2.isdigit()
False
  • Return string length
    len()
>>> str1 = 'asdqweadsadvsdasdcasdqqwecxz'
>>> len(str1)
28

In other sequences, it is used to return the number of elements

>>> tuple1 = (1, 2, 3, 4, 5, 8, 7, 6, 'asd')
>>> len(tuple1)
9
  • Replace character
    str.replace(old, new[, max]) replaces old with new, and the third parameter specifies the range
>>> str1 = 'asd sd qwe v d asd qwe g asd  a'
>>> str1.replace(' ','-')
'asd-sd-qwe-v-d-asd-qwe-g-asd--a'
# To hexadecimal spaces
>>> str1 = '02 12 45 48 32 15 45'
>>> str1.replace(' ','')
'02124548321545'

format

  • format

Unknown parameter format

>>> "{0}_{1}_{2}".format('ctf','is','fun')
'ctf_is_fun'

Keyword parameters

>>> "{b}_{a}_{c}".format(a='is', b='ctf', c='fun')
'ctf_is_fun'

Mixed use

>>> "{0}{{{b}_{a}_{c}}}".format('flag', a='is', b='ctf', c='fun')
'flag{ctf_is_fun}'
  • Format symbol
    Try string formatting with some symbols, which is unique to strings
Symbolexplain
%cCalligraphy and painting characters and ASCII code
%sformat string
%dFormat integer
%oFormat unsigned octal number
%xFormat unsigned hexadecimal number
%XFormat unsigned hexadecimal number (upper case)
%fFormat fixed-point number, which can refer to the precision after decimal
%eFormatting fixed-point numbers with scientific counting method
%Editto
%gUse% f or% e depending on the size of the value
%Gditto
  • %c
>>> '%c%c%c%c' % (102,108,97,103)
'flag'
  • %d
>>> '%d + %d = %d' % (3, 2, 2+3)
'3 + 2 = 5'

>>> '%d' % 0x10
'16'
  • Two interesting escape characters
    Octal: \ o
    Hex: \ x
  • Conversion between three sequences
# Use list to transform other sequences into lists
>>> str1 = 'asdasd'
>>> list1 = list(str1)
>>> list1
['a', 's', 'd', 'a', 's', 'd']

>>> tuple1 = (1, 2, 3, 4, 5, 6)
>>> list2 = list(tuple1)
>>> list2
[1, 2, 3, 4, 5, 6]
# Use tuple to convert other sequences into tuples
>>> list1 = [1, 2, 3, 4]
>>> tuple1 = tuple(list1)
>>> tuple1
(1, 2, 3, 4)
>>> str1 = 'asd123'
>>> tuple2 = tuple(str1)
>>> tuple2
('a', 's', 'd', '1', '2', '3')
# The last one is a little magical, just for reference
>>> list1 = [12, 45, 'asd']
>>> tuple1 = (555, 'qwe', 'z123')
>>> str1 = str(list1)
>>> str2 = str(tuple1)
>>> str1
"[12, 45, 'asd']"
>>> str2
"(555, 'qwe', 'z123')"

Tags: Python security

Posted on Sat, 04 Dec 2021 21:37:45 -0500 by ploiesti