Strings

Mon 30 June 2025
s = 'hello world'
print(len(s))
11
print(s.upper())
HELLO WORLD
print(s.lower())
hello world
print(s.capitalize())
Hello world
print(s.title())
Hello World
print(s.swapcase())
HELLO WORLD
print(s.startswith('hello'))
True
print(s.endswith('world'))
True
print(s.find('o'))
4
print(s.rfind('o'))
7
print(s.index('e'))
1
print(s.replace('world', 'python'))
hello python
print(s.split())
['hello', 'world']
print('-'.join(['a', 'b', 'c']))
a-b-c
print(s.strip())
hello world
print('  test  '.lstrip())
test
print('  test  '.rstrip())
  test
print(s.count('l'))
3
print('abc123'.isalnum())
True
print('abc'.isalpha())
True
print('123'.isdigit())
True
print('abc'.islower())
True
print('ABC'.isupper())
True
print('Hello'.istitle())
True
print('   '.isspace())
True
print('abc'.center(10, '-'))
---abc----
print('abc'.ljust(10, '*'))
abc*******
print('abc'.rjust(10, '*'))
*******abc
print('42'.zfill(5))
00042
print('a    b   c'.expandtabs(4))
a   b   c
print(f'Length of s is {len(s)}')
Length of s is 11
s2 = 'こんにちは'
print(s2.encode('utf-8'))
b'\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf'
print(list('hello'))
['h', 'e', 'l', 'l', 'o']
print(s[1:5])
ello
print(s[:5])
hello
print(s[6:])
world
print(s[::-1])
dlrow olleh
print('hello' * 3)
hellohellohello
print('lo' in s)
True
print('xyz' not in s)
True
print(any(char.isdigit() for char in s))
False
print(all(char.isalpha() for char in 'abc'))
True
print('python3'.partition('3'))
('python', '3', '')
print('foo
bar
baz'.splitlines())
  Cell In[78], line 1
    print('foo
          ^
SyntaxError: unterminated string literal (detected at line 1)
print('1,2,3'.split(','))
['1', '2', '3']
print('...hello...'.strip('.'))
hello
print('TestCase'.casefold())
testcase
print('ß'.lower())
ß
print('ß'.casefold())
ss
print(chr(97))
a
print(ord('a'))
97
print('hello'.__contains__('ell'))
True
print('python'.__getitem__(3))
h
print('hello'.__len__())
5
print('  abc  '.strip(' '))
abc
print('abcABC123'.isprintable())
True
print('abc
def'.isprintable())
  Cell In[91], line 1
    print('abc
          ^
SyntaxError: unterminated string literal (detected at line 1)
print('ABC'.rpartition('B'))
('A', 'B', 'C')
print('hello'.rindex('l'))
3
print('hello'.isidentifier())
True
print('_var'.isidentifier())
True
print('snake_case'.replace('_', ' ').title().replace(' ', ''))
SnakeCase
print('abc'.encode())
b'abc'
print(b'abc'.decode())
abc
print('abcDEF'.translate(str.maketrans('abc', '123')))
123DEF
print('12345'.isdigit())
True
print('Ⅻ'.isnumeric())
True
print('one1two2'.translate(str.maketrans('', '', '0123456789')))
onetwo
print('  Hello  '.strip().lower())
hello
print('12'.rjust(5, '0'))
00012
print('abc'.rjust(10))
       abc
print('xyxyxy'.replace('xy', 'z', 2))
zzxy
print('hello'.count('l', 1, 4))
2
print('abcdefghijklmno'.find('k', 5))
10
print('a-b-c'.rsplit('-', 1))
['a-b', 'c']
print('a,b,c'.split(',', 2))
['a', 'b', 'c']
print('abc'.rjust(6, '.'))
...abc
print(','.join(map(str, range(5))))
0,1,2,3,4
print('abc'.__add__('123'))
abc123
print('abc' < 'abd')
True
print('Abc' < 'abc')
True
print('hello'.endswith(('o', 'd')))
True
print('hello'.startswith(('he', 'le')))
True




Score: 85

Category: basics