Skip to content

CodinGame Tips

Working with Strings

Walking through a string

1
2
3
4
5
6
7
8
9
# Flip binary number
text = "1010111000"
newtext = ""
for s in text:
    if s == "0":
        newtext += "1"
    else:
        newtext += "0"
print(newtext)

Useful String functions

  • len()
  • startswith()
  • endswith()
1
2
3
4
s = "Hello World!"
print(len(s))
print(s.startswith("Hel"))
print(s.endswith("rld!"))
  • isalpha()
  • isnumeric()
  • islower()
  • isupper()
  • lower()
  • upper()
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Flip case of sentence
text = "This is a Mixed Case Sentence"
newtext= ""
for s in text:
    if s.isalpha():
        if s.isupper():
            newtext += s.lower()
        else:
            newtext += s.upper()
    else:
        newtext += s
print(newtext)

String slices

1
2
3
4
# Capitalize the first 2 and final 3 chars and convert the rest of the characters to lowercase
text = "this IS a SenTenCe that WE wIll use"
s = text[:2].upper() + text[2:-2].lower() + text[-3:].upper()
print(s)

Debugging

Print input values withoug interfering with the output with:

1
2
i = int(s.input())
print(i, file=sys.stderr)

Optimizing with Python 3

We can build up a string one character at time like this:

1
2
3
4
5
text = "this is input"
newtext = ""
for s in text:
    newtext += s.upper()
print(newtext)

Or, with Python3, we can pass an end argument to print() like this:

1
2
3
text = "this is input"
for s in text:
    print(s.upper(), end="")

Another example using the end parameter:

1
2
3
4
5
6
text = "this is input"
for s in text:
    print(i)

for s in text:
    print(i, end="")