Splitting strings in half, python [duplicate] - python-3.x

This question already has answers here:
Split a string into 2 in Python
(6 answers)
Closed 3 months ago.
I have this string my_string = '717460881855742062' how can I split it in half? The string is auto-generated so just splitting by the 1 won't work

You can try do this way:
firsthalf, secondhalf = my_string[:len(my_string)//2], my_string[len(my_string)//2:]

Something like this should do the job:
my_string = '717460881855742062'
length = int(len(my_string) / 2)
first_part = my_string[:length]
second_part = my_string[length:]
print(first_part)
print(second_part)
output:
717460881
855742062
You can modify it and make sure you take also handle the situation
where the length%2 is not 0.

Related

My .lower() and .upper() functions are not working. What is the problem here? [duplicate]

This question already has answers here:
python .lower() is not working [duplicate]
(3 answers)
Closed 1 year ago.
I don't know what I'm doing wrong here. Please help.
def AddColon (data):
data.strip() # --> to remove the spaces
result = ''
for i in range (len(data)):
if str(data[i]).isalpha(): # to only include letters
result = result + data[i]
result.lower() # to make everything lowercase
result[0].upper() # to make the first letter uppercase
finalresult = result + ' : Hello'
return finalresult
input1 = input('Insert Data : ')
print(AddColon(str(input1)))
If your input contains any numbers or integers it may not go under if condition and the result will not be updated. To get a exact value, u may need to give only strings as input not alphanumeric.After that remove the indentation in for loop and if condition...try removing that and run your code. Your code and output is ready.

How to generate a random order of numbers to a string based on conditions Python [duplicate]

This question already has answers here:
Generate random numbers only with specific digits
(3 answers)
Closed 2 years ago.
How would you generate a random number string in Python based on the following conditions.
The string must be between the length of 3 and 7
The string must only contain numbers from 1-7
The string must not have spaces
I tried the following for the string output but I am struggling with the conditionals
letters = string.digits
print ( ''.join(random.choice(letters) for i in range(10)) )
The output I received was:=
9432814671
If you could be kind enough to help me out and guide me I would be grateful to you.
The solution is self-explanatory, and you were close to it:
length = random.randint(3, 7)
"".join(str(random.randint(1, 7)) for _ in range(length))
#'724613'

How can i get the sum string using python? [duplicate]

This question already has answers here:
Python error: "IndexError: string index out of range"
(4 answers)
Closed 3 years ago.
Here is my program I want to find the weather it is a sum string or not based on the following condition
1)the string length must be >3
example:"12358" --- 1+2=3,2+3=5,3+5=8
I tried this program I am getting the index error please help me.Thank you in adavnce.
Given below is my code:
y="12358"
for i in range(len(y)-1):
if y[i]+y[i+1]==y[i+2]:
print "sum stringgg"
The upper bound of the range should be the length of y minus 2 instead to accommodate the comparison with the item of the index plus 2. You should also convert each character in the string to an integer for arithmetic addition and comparison. Finally, you should use the for-else construct to break whenever the former two digits do not add up to the latter digit, and only output 'sum string' if the loop finishes without break:
y = "12358"
digits = list(map(int, y))
for i in range(len(digits) - 2):
if digits[i] + digits[i + 1] != digits[i + 2]:
break
else:
print('sum string')

Swapping characters in a string [duplicate]

This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Return middle part of string
(2 answers)
Swap list / string around a character python
(1 answer)
Python code to swap first and last letters in string
(4 answers)
Write a function swap_halves(s) that takes a string s, and returns a new string in which the two halves of the string have been swapped
(3 answers)
Closed 3 years ago.
In python, how would you swap the first and last parts of a string but leave the middle character in the same place? For example, if I have ABCDE, then the result would be DECAB.
def swap(str1):
return str1[-1]+str1[1:-1]+str1[:1]
print(swap("Hello"))
With this, I am only able to swap the first and last letter of the string. So the result for 'Hello' is 'oellH'.
You can obtain the index of the middle character with the floor division //:
5 //2
# 2
So, you can change your code like this:
def swap(str1):
middle = len(str1) // 2
return str1[middle+1:] + str1[middle] + str1[:middle]
print(swap("ABCDE"))
# DECAB
Note that this only works for strings with an odd number of characters. We could modify the function to also handle strings with even numbers of chars, just swapping both halves in this case:
def swap(str1):
middle = len(str1) // 2
if len(str1) % 2 == 0:
# even length
return str1[middle:] + str1[:middle]
else:
# odd length
return str1[middle+1:] + str1[middle] + str1[:middle]
print(swap("ABCDE"))
# DECAB
print(swap("ABCDEF"))
# DEFABC

what is Fortran equivalent of the following statement [duplicate]

This question already has answers here:
Convert integers to strings to create output filenames at run time
(9 answers)
Closed 3 years ago.
I want to assign a formatted string to a variable. For example, I would write the following in Python:
my_score = 100
line = "score = %d" % my_score
print(line)
This will print the following:
score = 100
How to write the same in Fortran?
The direct implementation of Your code would be something like:
program test
integer :: score
character(len=30) :: line
score = 100
write(line, '(a, i3)') "score = ", score
print '(a)', line
end program test

Resources