>>> import re >>> secret = 'Sozialversicherungsnr.: 968127490567' >>> zahlen = re.compile('(0|1|2|3|4)') >>> print(re.sub(zahlen, 'X', secret)) Sozialversicherungsnr.: 968XX7X9X567 >>> zahlen = re.compile('(0|1|2|3|4|5|6|7|8|9)') >>> print(re.sub(zahlen, 'X', secret)) Sozialversicherungsnr.: XXXXXXXXXXXX >>> zahlen = re.compile('[0-9]') >>> print(re.sub(zahlen, 'X', secret)) Sozialversicherungsnr.: XXXXXXXXXXXX >>> zahlen = re.compile('[0123456789]') >>> print(re.sub(zahlen, 'X', secret)) Sozialversicherungsnr.: XXXXXXXXXXXX >>> zahlen = re.compile('[093751]') >>> print(re.sub(zahlen, 'X', secret)) Sozialversicherungsnr.: X68X2X4XXX6X >>> string = 'neuer string' >>> print(re.sub('^', 'Anfang: ', string)) Anfang: neuer string >>> sentences = 'Ein Satz. Noch einer.' >>> print(re.sub('.', ';', sentences)) ;;;;;;;;;;;;;;;;;;;;; >>> print(re.sub('\.', ';', sentences)) Ein Satz; Noch einer; >>> zahlen = re.compile('[093751]') >>> zahlen = re.compile('[^093751]') >>> print(re.sub(zahlen, 'X', secret)) XXXXXXXXXXXXXXXXXXXXXXXX9XX1X7X905X7 >>> zahlen = re.compile('[^\.;\?]') >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> string = 'dies ist ein Satz' >>> print(string.split(' ')) ['dies', 'ist', 'ein', 'Satz'] >>> query = 'casus=nom AND (genus=f OR genus=m) AND NOT numerus=sg' >>> operator2 = re.compile('([A-Z\(\)\s]+)') >>> query_zwischenergebnis = re.sub(operator2, ' ', query) >>> print(query_zwischenergebnis) casus=nom genus=f genus=m numerus=sg >>> print(query_zwischenergebnis.split()) ['casus=nom', 'genus=f', 'genus=m', 'numerus=sg'] >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> strasse = 'Straße: Privet Drive' >>> print(strasse.split(': ')) ['Straße', 'Privet Drive'] >>> strasse = 'Straße Privet Drive' >>> print(strasse.split(' ', 1)) ['Straße', 'Privet Drive'] >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> ip_good = '192.168.2.1' >>> ip_bad = '333.333.333.333' >>> ip_good_list = ip_good.split('.') >>> ip_good_list ['192', '168', '2', '1'] >>> for number in ip_good_list: ... if 0 < int(number) < 256: ... print('good') ... good good good good >>> ip_bad_list = ip_bad.split('.') >>> for number in ip_bad_list: ... if -1 < int(number) < 256: ... print('good') ... else: ... print('bad') ... bad bad bad bad >>> file = 'D:/Dateien/notizen.txt' >>> print(file.split('/')) ['D:', 'Dateien', 'notizen.txt'] >>> print(file.split('/')[-1]) notizen.txt >>> file = 'notizen.txt' >>> print(file.split('/')[-1]) notizen.txt >>> 'rentner'[::-1] 'rentner' >>> rentner.reverse() Traceback (most recent call last): File "", line 1, in NameError: name 'rentner' is not defined >>> 'rentner' == 'rentner'[::-1] True >>>