DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

String in Python (1)

Buy Me a Coffee

*Memos:

A string:

  • is the sequence of zero or more characters whose type is str.
  • is immutable so it cannot be changed.
  • can be created by the string literal '', "", '''''' or """""" or str() with or without any types of objects: *Memos:
    • A string literal can have a lot of Unicode characters.
    • A string literal can also be used for a docstring.
    • '' or "" is for one line.
    • '''''' or """""" is for one or more lines.
  • can be enlarged with * and a number.
  • can be accessed but cannot be changed by indexing or slicing.

Be careful, a huge string gets I/O error.


'' or "" can create the string which is a sequence of zero or more characters as shown below. *\' is the escape sequence to output ':

v = '' # Empty string
v = "Hello World"
v = "Lёт's gφ!" # Let's go!
v = "I'm John."
v = 'I\'m John.'
v = '''I'm John.'''
v = """I'm John."""
v = '''Apple Orange Banana Kiwi'''
v = 'Apple' " Orange" '''Banana''' """Kiwi"""
v = '''Apple
Orange
Banana
Kiwi'''
v = """
Apple
   Orange
       Banana
           Kiwi
"""
'These above get no error'
"These above get no error"
'''These above get no error'''
"""These above get no error"""
''' 
These above 
get no error 
'''
"""
These above 
get no error 
"""

print(type('Hello World'))
print(type(str('Hello World')))
# <class 'str'>
Enter fullscreen mode Exit fullscreen mode
v = '' # Empty string

print(v)
# Nothing
Enter fullscreen mode Exit fullscreen mode
v = "Hello World"

print(v)
# Hello World
Enter fullscreen mode Exit fullscreen mode
v = "Lёт's gφ!" # Let's go!

print(v)
# Lёт's gφ!
Enter fullscreen mode Exit fullscreen mode
v = "I'm John."
v = 'I\'m John.'
v = '''I'm John.'''
v = """I'm John."""

print(v)
# I'm John.
Enter fullscreen mode Exit fullscreen mode
v = '''Apple Orange Banana Kiwi'''
v = 'Apple' " Orange" ''' Banana''' """ Kiwi"""

print(v)
# Apple Orange Banana Kiwi
Enter fullscreen mode Exit fullscreen mode
v = '''Apple
Orange
Banana
Kiwi'''

print(v)
# Apple
# Orange
# Banana
# Kiwi
Enter fullscreen mode Exit fullscreen mode
v = """
Apple
   Orange
       Banana
           Kiwi
"""

print(v)
# 
# Apple
#    Orange
#        Banana
#            Kiwi
#
Enter fullscreen mode Exit fullscreen mode

A string can be enlarged with * and a number as shown below:

v = 'abc' * 3

print(v)
# abcabcabc
Enter fullscreen mode Exit fullscreen mode

Be careful, a huge string gets I/O error as shown below:

v = 'abc' * 100000000

print(v)
# OSError: [Errno 29] I/O error
Enter fullscreen mode Exit fullscreen mode

You can access a string by indexing or slicing as shown below:

v = 'abcdefgh'

print(v)
# abcdefgh

print(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7])
print(v[-8], v[-7], v[-6], v[-5], v[-4], v[-3], v[-2], v[-1])
# a b c d e f g h
Enter fullscreen mode Exit fullscreen mode
v = 'abcdefgh'

print(v[:])
print(v[::])
# abcdefgh

print(v[::2])
# aceg

print(v[::-2])
# hfdb

print(v[2:])
print(v[-6:])
print(v[2::])
print(v[-6::])
# cdefgh

print(v[2::2])
print(v[-6::2])
# ceg

print(v[2::-2])
print(v[-6::-2])
# ca

print(v[:6])
print(v[:-2])
print(v[:6:])
print(v[:-2:])
# abcdef

print(v[:6:2])
print(v[:-2:2])
# ace

print(v[:6:-2])
print(v[:-2:-2])
# h

print(v[2:6])
print(v[-6:-2])
print(v[2:6:])
print(v[-6:-2:])
# cdef

print(v[2:6:2])
print(v[-6:-2:2])
# ce

print(v[2:6:-2])
print(v[-6:-2:-2])
# Empty string
Enter fullscreen mode Exit fullscreen mode

You cannot change a string because it's immutable as shown below. *A del statement can still be used to remove a variable itself:

v = 'abcdef'

v[0] = 'X'
# v[-6] = 'X'
v[2:6] = ['Y', 'Z']
# TypeError: 'str' object does not support item assignment
Enter fullscreen mode Exit fullscreen mode
v = 'abcdef'

del v[0]
# del v[-6]
del v[3:5]
# TypeError: 'str' object does not support item deletion
Enter fullscreen mode Exit fullscreen mode
v = 'abcdef'

del v

print(v)
# NameError: name 'v' is not defined
Enter fullscreen mode Exit fullscreen mode

If you really want to change a string, use list() and join() as shown below. *join() can concatenate the zero or more strings in an iterable.

v = 'abcdef'

v = list(v)

v[0] = 'X'
# v[-6] = 'X'
v[2:6] = ['Y', 'Z']

v = ''.join(v)

print(v)
# XbYZ
Enter fullscreen mode Exit fullscreen mode
v = 'abcdef'

v = list(v)

del v[0]
# del v[-6]
del v[3:5]

v = ''.join(v)

print(v)
# bcd
Enter fullscreen mode Exit fullscreen mode

Top comments (0)