Python/String: Difference between revisions
< Python
Jump to navigation
Jump to search
| (21 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
= Using statements = | = Using statements = | ||
{| class="wikitable" | {| class="wikitable" | ||
! | ! || Code | ||
|- | |- | ||
| | | Length | ||
| | |||
<source lang="python"> | |||
len(s) | |||
</source> | |||
|- | |- | ||
| | | Substring | ||
| <source lang="python"> | |||
s = '2018-01-01' | |||
s[:4] | |||
s[5:] | |||
s[5:7] | |||
</source> | |||
|- | |- | ||
| Has substring | |||
| <source lang="python"> | |||
if '18' in '2018-01-01': ... | |||
</source> | |||
|} | |} | ||
| Line 13: | Line 27: | ||
! || Sample | ! || Sample | ||
|- | |- | ||
| | | bytes => str | ||
| <source lang="python"> | |||
b'abc'.decode('utf-8') | |||
</source> | |||
|- | |||
| Prefix & Suffix | |||
| <source lang="python"> | |||
if s.startswith(): ... | |||
if s.endswith(): ... | |||
</source> | |||
|- | |- | ||
| | | | Formatting | ||
| <source lang="python"> | |||
s = '{} seconds elapsed'.format(time.time() - begin) | |||
</source> | |||
|- | |- | ||
| To Join & Split | |||
| <source lang="python"> | |||
'.'.join((127,0,0,1)) | |||
','.join((a,b,c)) | |||
':'.join((user,group)) | |||
</source> | |||
|- | |||
| To trim | |||
| <source lang="python"> | |||
s = s.trim() | |||
s = s.ltrim() | |||
s = s.rtrim() | |||
</source> | |||
|} | |||
= Printing = | |||
{| class="wikitable" | |||
! TODO || Code | |||
|- | |||
| Without newline || | |||
<source lang="python3"> | |||
print('Result is: ', end='') | |||
print('passed') | |||
</source> | |||
|- | |||
| With colors || | |||
<source lang="python3"> | |||
print('\033[91mFailed\033[0m') | |||
print('\033[92mPassed\033[0m') | |||
</source> | |||
|- | |||
| Load printed text || | |||
<source lang="python3"> | |||
stdout_backup = sys.stdout | |||
sbuf = io.StringIO() | |||
sys.stdout = sbuf | |||
print('previous message') | |||
sbuf.seek(0) | |||
printed_text = sbuf.read().rstrip() | |||
sbuf.close() | |||
sys.stdout = stdout_backup | |||
print('Text printed previously:') | |||
print(printed_text) | |||
</source> | |||
|} | |} | ||
Latest revision as of 06:44, 25 April 2019
Using statements
| Code | |
|---|---|
| Length |
len(s)
|
| Substring | s = '2018-01-01'
s[:4]
s[5:]
s[5:7]
|
| Has substring | if '18' in '2018-01-01': ...
|
Using methods
| Sample | |
|---|---|
| bytes => str | b'abc'.decode('utf-8')
|
| Prefix & Suffix | if s.startswith(): ...
if s.endswith(): ...
|
| Formatting | s = '{} seconds elapsed'.format(time.time() - begin)
|
| To Join & Split | '.'.join((127,0,0,1))
','.join((a,b,c))
':'.join((user,group))
|
| To trim | s = s.trim()
s = s.ltrim()
s = s.rtrim()
|
Printing
| TODO | Code |
|---|---|
| Without newline |
print('Result is: ', end='')
print('passed')
|
| With colors |
print('\033[91mFailed\033[0m')
print('\033[92mPassed\033[0m')
|
| Load printed text |
stdout_backup = sys.stdout
sbuf = io.StringIO()
sys.stdout = sbuf
print('previous message')
sbuf.seek(0)
printed_text = sbuf.read().rstrip()
sbuf.close()
sys.stdout = stdout_backup
print('Text printed previously:')
print(printed_text)
|