Python/String: Difference between revisions

From Fundamental Ramen
Jump to navigation Jump to search
No edit summary
Line 57: Line 57:
</source>
</source>
|}
|}
= Print without newline =
<source lang="python3">
print('Result is: ', end='')
print('passed')
</source>

Revision as of 06:20, 7 June 2018

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()

Print without newline

print('Result is: ', end='')
print('passed')