[PYTHON] 00. Reverse order of strings

00. Reverse order of strings

Get a string in which the characters of the string "stressed" are arranged in reverse (from the end to the beginning).

Go

package main

import_ "fmt"

func main() {
	var data string = "stressed"
	var reverse string

	// 1.loop
	for i := len(data) - 1; i >= 0; i-- {
		reverse += string(data[i])
	}

	fmt.Println(reverse)
}

python


# -*- coding: utf-8 -*-
data = "stressed"
reverse = ""

#   1.Reverse count loop
i = len(data) - 1
while i >= 0:
    reverse += data[i]
    i-=1
print(reverse)

#   2.range->reversed
reverse = ""
for i in reversed(range(len(data))):
    reverse += data[i]
print(reverse)

#   3.reversed->join
print(''.join(reversed(data)))

#   4.slice
print(data[::-1])

Javascript

var data = "stressed";
var reverse = ""

//  1.Reverse count loop
for (var i = data.length - 1; i >= 0; i--) {
    reverse += data[i];
}
console.log(reverse);

//  2.split->reverse->join
console.debug(data.split("").reverse().join(""));

Summary

go does not support Japanese. Problem page and top page split.

Top

Recommended Posts

00. Reverse order of strings
Solve 100 language processing knocks 2020 (00. Reverse order of character strings)
Concatenation of ruby strings
Reverse strings in Python
Order of arguments of RegularGridInterpolator and interp2d
Replacement of strings containing Linux spaces
[Excel] Addition of character strings (combination)
Change the length of Python csv strings
Arbitrarily rearrange the column order of Pandas.DataFrame
Bulk replacement of strings in Python arrays
Change the order of PostgreSQL on Heroku
[Python] Various combinations of strings and values