0

I have following problem: I want to split a string into half that always has different characters and always the same length. How would I do this since I can't specify a character with split().

import secrets

# generate string
string = secrets.token_hex(32)

# write string into file
write_string = open("string_file", "w").write(string)

# read and split string
read_string1 = open("string_file", "r").readline(32)
print(read_string1)

# i can only specify readline from 0-32 but how can I specify reading from 33-64 ?
read_string2 = open("string_file", "r").readline()
print(read_string2)
0

3 Answers 3

0

How about :

randomStr = "sadfwoerowienwdfk"
halfway = int(len(randomStr)/2)
# prints first half followed by second half
print(randomStr[:halfway], randomStr[halfway:])
Sign up to request clarification or add additional context in comments.

Comments

0

If you are looking for a one-liner:

string = "psjfdobhdcgspobhcpgdmj"


def split_halfway(string):
    return (string[:int(len(string) / 2)], string[int(len(string) / 2):])


print(split_halfway(string))

This function returns the first half followed by the second half

Comments

-1

try:

import secrets


string = "your_string"
print(string[0: int(len(string)/2)])

this'll just split it in half

1 Comment

string = your_preferred_string

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.